例:
If condition or _
condition or _
condition or _
condition or _
condition or _
condition or _
condition or _
condition or _
condition or _
condition or Then
Do something
End If
假设我有超过这10个条件我需要评估...有没有比嵌套多个这些if
语句更好的方法?
这是一个选项 - 一次做一个测试,用布尔值跟踪最终结果。完成所有操作后,只需测试布尔值即可。
Dim A As Long
Dim B As Long
Dim C As Long
Dim D As Long
Dim Result As Boolean
Result = True
Result = Result And (A > 10)
Result = Result And (B > 10)
Result = Result And (C > 10)
Result = Result And (D > 10)
If Result Then
' Do "something" here...
End If
如果A,B,C或D中的任何一个小于10,Result
将翻转到False
并从那时起保持这种状态。如果所有测试都通过,它将只是True
。
您可以使用Case语句。它比if
s更清洁:http://msdn.microsoft.com/en-us/library/cy37t14y%28v=vs.80%29.aspx
Dim Result as boolean
result = false
If condition1 Then result = true
ElseIf condition2 Then result = true
ElseIf condition3 Then result = true
ElseIf condition4 Then result = true
If result Then debug.print "Success"
如果您想使用条件不相同的select语句,请使用:
Select Case True
Case A=5,b=10,c="my answer",d=11
....
End Select