I have an if
statement with multiple conditions. I cannot see them all in a single window view. Is there a way to separate them on different lines or do they have to be written all in one line?
问题:
回答1:
The VBA line-continuation character is an underscore _
if ( _
(something) _
or (somethingelse) _
)
回答2:
You can use the line continuation character _
These are all the same:
If Something Or SomethingElse Or AnotherThing Then
If Something Or SomethingElse _
Or AnotherThing Then
If Something Or _
SomethingElse Or _
AnotherThing Then
回答3:
Like the above, you can break up the long set of conditions in one "IF" statement by using an underscore.
If there are too many conditions and I find it challenging to read, I use BOOLEANS to represent the conditions. It seems like there is more code, but I find it easier to read.
If I have a scenario where I need to do the following:
IF (X = something1) OR (X = something2) OR (X = something2) OR (X = something2) OR (X = something2) OR (X = something2) OR (X = something2) OR (X = something2) THEN
END IF
I could do this:
IF _
(X = something1) _
OR (X = something2) _
OR (X = something3) _
OR (X = something4) _
OR (X = something5) _
OR (X = something6) _
OR (X = something7) _
OR (X = something8) _
THEN
END IF
... or I could do this ...
blnCondition1 = (X = something1)
blnCondition2 = (X = something2)
blnCondition3 = (X = something3)
blnCondition4 = (X = something4)
blnCondition5 = (X = something5)
blnCondition6 = (X = something6)
blnCondition7 = (X = something7)
blnCondition8 = (X = something8)
IF blnCondition1 OR blnCondition2 OR blnCondition3 OR blnCondition4 OR blnCondition5 OR blnCondition6 OR blnCondition7 OR blnCondition8 THEN
END IF
Of course, the example BOOLEAN variables are defined, and instead of names like blnCondition1, I'll use meaningful names.
Again, it's just a preference I have.
回答4:
break them with an under score _ ?
Microsoft Library
from above link
If ActiveSheet.ChartObjects(1).Chart.ChartTitle = _
ActiveSheet.Range("a2").Value Then
MsgBox "They are equal."
End If
回答5:
im pretty sure you can use an underscore _
to break up lines.