Question

Example:

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

Say I have more than these 10 conditions I need to evaluate... Is there a better way than just nesting multiple sets of these if statements?

Was it helpful?

Solution

Here's an option -- do one test at a time, tracking the final result in a boolean. When you're all done, just test the boolean.

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

If any of A, B, C, or D is less than 10, Result will flip to False and stay that way from then on. It will only be True if all of the tests pass.

OTHER TIPS

You could use Case statements. It's a little bit cleaner than ifs: 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"

If you wanted to use a select statement where conditions are not the same then use:

Select Case True
    Case A=5,b=10,c="my answer",d=11
      ....
End Select
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top