What are the underscores _ at the end of lines of code there for?

Article contributed by Dave Rado

A space followed by an underscore tells VBA that the current statement isn't finished yet but continues on the next line – it's used to split a single line of code over two lines, in order to make the code more readable (because VBA doesn't word-wrap).

So rather than:

If Not MyRange.Paragraphs(1).Range.End = MyRange.Sections(1).Range.End Then

you could use:

If Not MyRange.Paragraphs(1).Range.End _
 = MyRange.Sections(1).Range.End Then

But you must insert a space before the underscore. Otherwise (in the above example) the VBA compiler would assume you thought there was such a word as End_ and would  return an error.


Click to view Terms of Use page

Click to view Disclaimer page