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,421
- 1
- 14
- 27

- 4,594
- 9
- 33
- 61
-
I think I must be the only person who rathers it all on one line, even many pages wide, I just find it hard to follow broken in to seperate lines – DevilWAH Nov 28 '11 at 23:28
5 Answers
The VBA line-continuation character is an underscore _
if ( _
(something) _
or (somethingelse) _
)

- 5,858
- 5
- 46
- 68
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

- 123,280
- 14
- 225
- 444
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.

- 431
- 5
- 10
break them with an under score _ ?
from above link
If ActiveSheet.ChartObjects(1).Chart.ChartTitle = _
ActiveSheet.Range("a2").Value Then
MsgBox "They are equal."
End If

- 3,865
- 1
- 27
- 50

- 2,553
- 13
- 41
- 57
im pretty sure you can use an underscore _
to break up lines.

- 3,049
- 1
- 16
- 12
-
-
from memory it can be a little picky as to where you are allowed to break up a line. – smitec Nov 28 '11 at 23:28
-