Resting Anchor

The Anchorage

Personal website of Gregory K. Maxey, Commander USN (Retired)

Break VBA Code Lines Using Underscore " _"
(A Microsoft Word Help & Tip page by Gregory K. Maxey)

DISCLAIMER/TERMS OF USE

The information, illustrations and code contained in my "Microsoft Word Tips" are provided free and without risk or obligation.

Click to acces PayPal Verification Service Click to acces PayPal Verification Service

However, the work is mine. If you use it for commercial purposes or benefit from my efforts through income earned or time saved then a donation, however small, will help to ensure the continued availability of this resource.

If you would like to donate, please use the appropriate donate button to access PayPal. Thank you!

Click to donate British Pound Sterling                   Click to donate US dollars                   Click to donate EU euros

The purpose of this Microsoft Word Tips & Microsoft Word Help page is to demonstrate and explain what an underscore " _" is typically used for in a in a VBA code line.

An underscore (always immediately preceded with a single space) in a VBA code line indicates that the current statement continues on the next line.  Because VBA doesn't word-wrap, it is used to split a single line of code over two lines, in order to make the code more readable.

So instead of this:

VBA Script:
Sub Macro1()
  Msgbox "Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal. 
End Sub  

We could write this:

VBA Script:
Sub Macro1()
  Msgbox "Four score and seven years ago our fathers " _
         & "brought forth on this continent, a new nation, " _
         & "conceived in Liberty, and dedicated to the proposition " _
         & "that all men are created equal." 
End Sub

Or rewrite:

VBA Script:
Sub Macro2()
Dim oRng As Range
  Set oRng = ActiveDocument.SelectContentControlsByTitle("RichText Container").Item(1).Range.Paragraphs(3).Range
End Sub

As:

VBA Script:
Sub Macro2()
Dim oRng As Range
  Set oRng = ActiveDocument.SelectContentControlsByTitle("RichText Container") _
           .Item(1).Range.Paragraphs(3).Range
End Sub

Site Note Icon Note: you must insert a space before the underscore. Otherwise the VBA compiler returns an error.

That's it! I hope you have found this tips page useful and informative.

Share

DISCLAIMER/TERMS OF USE

The information, illustrations and code contained in my "Microsoft Word Tips" are provided free and without risk or obligation.

Click to acces PayPal Verification Service Click to acces PayPal Verification Service

However, the work is mine. If you use it for commercial purposes or benefit from my efforts through income earned or time saved then a donation, however small, will help to ensure the continued availability of this resource.

If you would like to donate, please use the appropriate donate button to access PayPal. Thank you!

Click to donate British Pound Sterling                   Click to donate US dollars                   Click to donate EU euros

Search my site or the web using Google Search Engine

Google Search Logo