The Anchorage
Personal website of Gregory K. Maxey, Commander USN (Retired)
The information, illustrations and code contained in my "Microsoft Word Tips" are provided free and without risk or obligation.
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!
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:
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:
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:
Sub Macro2() Dim oRng As Range Set oRng = ActiveDocument.SelectContentControlsByTitle("RichText Container").Item(1).Range.Paragraphs(3).Range End Sub
As:
Sub Macro2() Dim oRng As Range Set oRng = ActiveDocument.SelectContentControlsByTitle("RichText Container") _ .Item(1).Range.Paragraphs(3).Range End Sub
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.
The information, illustrations and code contained in my "Microsoft Word Tips" are provided free and without risk or obligation.
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!