Fresh Excel Tips

Quick and Easy Excel Tips and Tutorials

VBA Commenting and Line Continuation

Comments are very useful for tracking and documenting you code in VBA. Any text that follows an apostrophe is considered a comment and is always ignored by Excel. For example, the first line in the following code is a comment, as is everything following the apostrophe on the third line:

‘ Declare a string variable
Dim FreshExcel as String
FreshExcel = Activesheet.Name ‘ Get name of active sheet

When debugging code, one useful trick that is often used is to temporarily comment out lines of code so they will not execute. The lines can be later uncommented to restore them to the code. The CommentBlock and UncommentBlock buttons, which can be found on the Edit toolbar, will place or remove comment marks from each currently selected line of code and are very useful for commenting out several lines of code in one step.These commands can be added to a menu and given menu accelerator keys to speed up the process.

Line Continuation

Long lines of code are a common sight for many VBA Programmers. These can be difficult to read especially when you will need to scroll horizontally to see an entire line. Because of this,  a line-continuation character was added into VBA. This character is the underscore, which must be followed by a space and cannot be followed by any other characters (including comments). For example, the following code:

ActiveSheet.Range(”A1″).Font.Bold = _
True

will be treated as one line by Excel. It is important to note that a line continuation character cannot be inserted in the middle of a literal string constant, which is enclosed in quotation marks.

VBA Commenting and Line Continuation
  1. How to Use the SLN function to calculate straight-line depreciation
  2. How to Write Subroutines Using VBA in Excel
  3. How to Write Your Own Objects in Excel VBA
  4. How to Use IF/then/Else Conditional Statements in Excel VBA
  5. Summary of the most Commonly used VBA Built-in Functions

Leave a Reply