Line Continuation Character

Lines of VBA code can often be quite long and difficult to read so you are encouraged to use the line continuation character.
For any lines of code that are quite long you can use the line continuation character to split them into several lines.
The VBA Editor only allows 1023 characters per line.
VBA does not word wrap.
The line continuation character "_" is actually two characters.
There must always be a space before the underscore.
Remember it is possible to have multiple instructions on a single line by using a colon ":" to separate them.
The following two lines is valid syntax but very difficult to read and should be avoided.

Sub DoSomething(): Dim iNumber1 As Integer: Dim iNumber2 As Integer: _ 
iNumber1 = 5: iNumber2 = 10: Call MsgBox(iNumber1 + iNumber2): End Sub

Examples

The line continuation character cannot be followed by any other characters (including comments).
You cannot have a line continuation character in the middle of a literal string.


Maximum of 24

You cannot include more than 24 line continuation characters in a single expression.

If you ever run into this limitation, you should use string concatenation instead.

Public Sub LineContinuation() 
Dim sMyString As String
    sMyString = "one" & _
                "two" & _
                "three" & _
                "four" & _
                "five" & _
                "six" & _
                "seven" & _
                "eight" & _
                "nine" & _
                "ten" & _
                "eleven" & _
                "twelve" & _
                "thirteen" & _
                "fourteen" & _
                "fifteen" & _
                "sixteen" & _
                "seventeen" & _
                "eighteen" & _
                "nineteen" & _
                "twenty" & _
                "twentyOne" & _
                "twentyTwo" & _
                "twentyThree" & _
                "twentyFour" & _
                "twentyFive" & _
End Sub

If you ever run into this limitation, you should consider passing in an object.

Public Sub PassingInArguments(byval iOne as Integer, _ 
                              byval iTwo as Integer, _
                              byval iThree as Integer, _
                              byval iFour as Integer, _
                              byval iFive as Integer, _
                              byval iSix as Integer _
                              byval iSeven as Integer, _
                              byval iEight as Integer, _
                              byval iNine as Integer, _
                              byval iTen as Integer, _
                              byval iEleven as Integer, _
                              byval iTwelve as Integer, _
                              byval iThirteen as Integer, _
                              byval iFourteen as Integer, _
                              byval iFifteen as Integer, _
                              byval iSixteen as Integer, _
                              byval iSeventeen as Integer, _
                              byval iEighteen as Integer, _
                              byval iNineteen as Integer, _
                              byval iTwenty as Integer, _
                              byval iTwentyOne as Integer, _
                              byval iTwentyTwo as Integer, _
                              byval iTwentyThree as Integer, _
                              byval iTwentyFour as Integer, _
                              byval iTwentyFive as Integer, _
End Sub

© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext