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.
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.
If you really want to learn VBA, grab a copy of our Ultimate VBA Training Bundle before reading our next tutorial. We specifically created these cheat sheets to help you get the most out of our upcoming lessons. Together, the set has over 200 practical tips covering the 125 most important topics in Excel VBA. We also stuffed it with 140 helpful macro examples.