How To Add Comments/Notes To Your VBA Code
How To Insert A VBA Comment/Note
Microsoft's VBA coding language allows you to create non-executable lines of code for commenting or note-taking. Incorporating comments into your VBA macro code is almost as important as the code itself. These notations will allow you to remember the purpose of your code and help others understand your thought process while troubleshooting or modifying your VBA macro in the future.
To add a comment to your VBA macro code, simply type an apostrophe ( ' ) and start typing. All the remaining text in that code line will be considered a comment (non-executing code) and will typically turn a green color once you navigate to the next line.
The VBA language truly has the simplest way to convert a line of code into a comment!
Steps to Insert A VBA Comment:
- Place your cursor at the very beginning of the line you wish to comment
- Type an apostrophe symbol
- Either click off the line or continue typing out your VBA comment
- Once you have navigated to a new line, the font should turn green (indicating a code comment)
You’ll visually see that your VBA code has turned into a comment as it will change its font color to green after you navigate to a different or new line in your code. Many people have gotten confused by the fact that the comment format does not change until after you click off the commented line.
Adding Comments Beside Code
As an alternative to creating entire lines of commented text, you also have the capability to place comments to the side of your code. This may be practical if you want to notate an alternate value to a variable input or if you have a comment that is only a few words.
The only stipulation for adding a comment next to your VBA code is the remainder of the code line will be commented on. You cannot have comments stuck in between code text on the same line.
How To Comment A Section Of VBA Code All At Once
There may be times when you are writing your code that you wish to prevent or turn off a section while testing. This typically would require you to insert an apostrophe character in front of each line of the code you wish to turn off.
The problem with this is if you have many lines of code that you want to comment out, this could take quite a lot of manual work (especially if you need to go back and forth between each state).
Luckily, Microsoft's Visual Basic Editor has a toolbar button that will allow you to comment out an entire selection of text with just one click. This button is called the Comment Block button and it resides in the Edit toolbar.
By default, the Edit Toolbar may not be visible in your Visual Basic Editor. To ensure you have access to it, navigate to the View menu, select the Toolbars menu button, and ensure that the Edit button has a check next to it (click it if it doesn't).
The Comment Block button is the 9th button in the Edit Toolbar. By clicking the Comment Block button, any code you have selected will automatically be commented by inserting an apostrophe in front of each line of text.
NOTE: The Comment Block button only places an apostrophe at the beginning of a code line. If you attempt to select part of a line, the entire code line will be commented out.
How To Remove Comments From A Section of VBA Code
It would not be fair if you could add comments to a large section of code all at once and not remove the comments in a similar fashion. That would just be cruel and unusual punishment!
To remove all the apostrophes from the beginning of your selected code lines, you can click the Uncomment Block button. This button is right beside the Comment Block button in the Edit Toolbar.
NOTE: The Uncomment Block button only removes the apostrophes from the beginning of code lines. If you have comments beside the VBA code on the same line, that comment's apostrophe will not be removed by the Uncomment Block button.
How To Create A Multi-Line Comment
While not very common, there is a way to create a multi-line comment. This technique is virtually identical to how you write multi-lined VBA code.
By inserting a space + underscore at the end of your commented code, you indicate that you would like the following line to also be part of the same commented text. You can repeat this as many times as you'd like to create multi-lined notes with the use of a single apostrophe character.
Change The Color Format Of VBA Comments
By default commented text is a green color. If you would like to change this in your Visual Basic Editor (this does not get passed along to others), you can do the following:
- Open the Tools menu in the Visual Basic Editor
- Click Options…
- Navigate to the Editor Format Tab
- In the Code Colors Listbox, select Comment Text
- Modify your desired color formats
- Foreground Color represents the font text color
- Background Color represents the fill color behind the text (like a highlight)
- Click the OK button
How To Create Keyboard Shortcuts For VBA Commenting
Unfortunately, there are no out-of-the-box keyboard shortcuts set up for the Comment Block and Uncomment Block buttons. There are, however, toolbar customizations you can tweak to assign a keyboard shortcut to these functions.
The concept behind assigning keyboard shortcuts in the Visual Basic Editor is renaming buttons with an “&” symbol along with a letter. This translates to the keyboard shortcut Alt + [insert your letter].
For example, we could rename the Comment Block button to be titled &C which would allow us to call that button’s functionality with the keyboard shortcut Alt + C.
How To Customize A Toolbar Button In The VBE
Let’s walk through how to customize the Comment Block button so that it has a keyboard shortcut that we can call.
- Right-click on a toolbar button and select Customize...
- Select the Comment Block button so that it has a thick black box around it
- Select the Commands tab
- Click the Modify Selection menu button
- Place and “&” character at the beginning of the Name field along with a letter you wish to include as the shortcut key
- Select the Image and Text menu item so that it has a checkmark beside it
- Close out of the Customize dialog box
Now you should see your Comment Block button modified. You will not see the “&” character you entered into the Name Field. Instead, you will see the first character in the button name underlined. This indicates that the button now has a keyboard shortcut assigned to it.
In this example, the keyboard shortcut is Alt + C.
If you don’t want too much text showing in your toolbar, you can forgo the full label and just include the shortcut letter. In the above example, I used “&C” and “&U” as my button label text so that my toolbar didn’t have too much text in it.
I Hope This Helped!
Hopefully, I was able to explain how you can use VBA commenting within your macro code. If you have any questions about this technique or suggestions on how to improve it, please let me know in the comments section below.
After 10+ years of creating macros and developing add-ins, I've compiled all the hacks I wish I had known years ago!
Top Microsoft Excel Add-ins You Should Consider Using
What Are Add-ins? Add-ins for Excel are similar to the apps you can download and purchase for your smartphone. They...
VBA Function To Determine What Is Currently Selected On Spreadsheet
What This VBA Macro Does This VBA function is a handy piece of code that categorizes what is currently selected...
Chris is a finance professional and Excel MVP recognized by Microsoft since 2016. With his expertise, he founded TheSpreadsheetGuru blog to help fellow Excel users, where he shares his vast creative solutions & expertise. In addition, he has developed over 7 widely-used Excel Add-ins that have been embraced by individuals and companies worldwide.