As you read the code examples, you often encounter the comment symbol ('
). This symbol tells the Visual Basic compiler to ignore the text following it, or the comment. Comments are brief explanatory notes added to code for the benefit of those reading it.
It is good programming practice to begin all procedures with a brief comment describing the functional characteristics of the procedure (what it does). This is for your own benefit and the benefit of anyone else who examines the code. You should separate the implementation details (how the procedure does it) from comments that describe the functional characteristics. When you include implementation details in the description, remember to update them when you update the function.
Comments can follow a statement on the same line, or occupy an entire line. Both are illustrated in the following code.
When editing a Macro in VBA, I currently click the icon to 'comment out'. How to comment and uncomment blocks of code in the Office VBA Editor. My hundreds of mouse clicks, registered on Mr.excel just to say thanks. Sep 26, 2013 Commenting a block of code in VBA. One way is to manually place an apostrophe at the start of every line in the block. A much easier way (I just found out today) is: Drag the “Comment Block” and “Uncomment Block” icons onto your toolbar.
If your comment requires more than one line, use the comment symbol on each line, as the following example illustrates. Serial number for vue scan 9 x64.
Commenting Guidelines
The following table provides general guidelines for what types of comments can precede a section of code. These are suggestions; Visual Basic does not enforce rules for adding comments. Write what works best, both for you and for anyone else who reads your code.
Comment type | Comment description |
Purpose | Describes what the procedure does (not how it does it) |
Assumptions | Lists each external variable, control, open file, or other element accessed by the procedure |
Effects | Lists each affected external variable, control, or file, and the effect it has (only if it is not obvious) |
Inputs | Specifies the purpose of the argument |
Returns | Explains the values returned by the procedure |
Remember the following points:
Every important variable declaration should be preceded by a comment describing the use of the variable being declared.
Variables, controls, and procedures should be named clearly enough that commenting is needed only for complex implementation details.
Comments cannot follow a line-continuation sequence on the same line.
You can add or remove comment symbols for a block of code by selecting one or more lines of code and choosing the Comment () and Uncomment () buttons on the Edit toolbar.
Note
You can also add comments to your code by preceding the text with the REM
keyword. However, the '
symbol and the Comment/Uncomment buttons are easier to use and require less space and memory.
See also
Single Line Comments Block Comments
Today, I’ll show you how to add comments to your Visual Basic Editor.
My goal with these Excel VBA tutorials is to get you to the point where you’re able to write macros with your eyes closed. Once you start writing complex macros, you’ll appreciate the little notes you embed deep within your code. It’s easy to get lost in your macros, especially if you haven’t looked at them in months.
Single Line Comments
Any text appearing after an apostrophe will be ignored by the VBA interpreter. The example below shows comments added to the Chapter 1.3 macro.
Commented lines show up green in the Visual Basic Editor. You can use the apostrophe to comment out entire lines or to comment characters at the end of a line.
Comment Blocks
Sometimes you have entire blocks of code you want to skip over. You can comment out the block with the aptly named “Comment Block” feature. For such a useful feature, it bothers me that this isn’t readily-accessible by default. To access the comment block feature, follow these steps:
- From the Visual Basic Editor, click View
- Hover over Toolbars
- Click Edit
You may want to dock the Edit toolbar to the top of your editor; it can come in handy. - Highlight the block of code you want to comment
- Click the Comment Block button
Your entire block of highlighted code will now be fully commented out.
To uncomment the code, highlight the code and click the Uncomment Block button to the right of the Comment Block button you clicked earlier.
In this lesson you learned how to comment individual lines and how to comment entire blocks of code at once. Commenting is a fundamental skill that, if used appropriately, will greatly enhance the readability of your Excel macros.