Once you start writing VBA codes, there’s an important thing that you need to learn along with that and that’s using COMMENTS in your VBA codes.
The point is: Using VBA COMMENT is quite easy and simple, the only thing you need to learn is to do it effectively.
So today, I’ll be sharing with you all the details about using comments in VBA and all the options related to them.
What is a VBA Comment
A VBA COMMENT is a green line of text that helps you to describe the written code. In simple words, a comment is a line of text which is not a code and VBA ignores it while executing the code. It’s a good practice (I’d say one of the best) to add comments in your VBA codes.
(Video) Understanding VBA Comments
Advantages of using a Comment
As I said commenting in a VBA code is one of the best practices and there are a few benefits that come with it.
- Helps you to Document your Work: You can use a comment to describe how code works, which can help you in the future to recall it easily or any other user.
- Track the Changes: If some codes need you to change them frequently you can use comments to track or record changes within the code.
- Describe a Function Procedure: When you write a procedure you can add a comment at the starting to describe the purpose of this procedure and how it works.
- Describe a Variable: Variables are one of the most important things that you need to use while writing a VBA code and you can use a comment to describe a variable.
- Debug Your Code: You can use VBA comments to debug the code by converting code lines into comments for testing.
Add a Comment in a VBA Code
Steps you need to follow to add a comment in a VBA code:
- First, click on the line where you want to insert the comment.
- After that, type an APOSTROPHE using your keyboard key.
- Next, type the comment that you want to add to the code.
- In the end, hit enter to move to the new line and the comment will turn green.
The moment you do this the entire line of the code will turn green which means that line is comment now. If you look at the below code where I have used a comment to add a description of the procedure.
You simply need to add an apostrophe before turning it into a comment and VBA will ignore it while executing the code.
Use the Comment/UnComment Button from the Toolbar
The second method is to use the comment block button from the toolbar. This button simply adds an apostrophe at the start of the line.
To use this button, first of all, you need to select the line of the code and then click on the button.
Next to the Comment button, there’s one more button “Uncomment” which you can use to uncomment a line (This button simply removes the apostrophe from the line of the code).
Enter a Multi-Line VBA Comment
There could be a situation where you need to enter a comment in multiple lines, like a block of the comments.
But here is one thing which you need to note down, every line of comment needs to start with an apostrophe, so if you want to add multiple lines of comments every line should have an APOSTROPHE.
The easiest way is to select all the lines and then use the comment button from the toolbar or you can also add an APOSTROPHE at the starting of each line.
The moment you click the comment button it will convert all the lines into a multi-line comment block.
Update: There’s one thing that I have discovered recently if you have a block of comment line (continuous), you can use a line continuation character (an underscore must be immediately preceded by a space).
In the above example, I have used an apostrophe only at the start of the first line of the comment, the rest two-line don’t have an apostrophe but I have used line continuation character to give a line break at the end of the first line and the second line.
Use the “REM” Keyword to Insert a Comment in VBA
This is the third way to insert a comment into a VBA code. Well, this is not a popular way but still, you can use it. So instead of using an apostrophe, you can use the keyword REM at the starting of a comment line.
REM stands for remarks.
Now, in the above example, I have used “REM” at the start of the line of the code and then the line of the code. But, there’s no button to add REM, you have to type it.
Comments while Recording a Macro with Macro Recorder
When you record a macro code using the macro recorder, you get an option to add a description before you record it.
So when you open the record macro dialog box, there is a “Description” input box where you can add your comment and then start recording.
And when you open the VBE to see the recorded code, you can see the code which you have added as a comment.
When you add a comment while recording a macro, VBA adds a few apostrophes as blank comments.
Enter a Comment in the Same Line
This is a quite smart way to use a comment in the same line where you have written code. In the below example you can see that I have three lines of code, where I have added comments after each line to describe it.
So once you complete a line of code you can use enter a comment after that in the same line.
Shortcut Key for Add a Comment
Truly speaking there’s no (by default keyboard shortcut) to use to insert a comment. But thanks to Gaurav, I have found a way to create a shortcut key to insert an apostrophe.
Follow the below steps:
- First of all, right-click on the toolbar and then click on the customize option.
- In the customize dialog box, go to the Command-Tab ➜ Edit ➜ select the comment block, and drag it to the toolbar.
- After that select the comment block icon, like below and right-click on it, and then enter “&” before the name. So it should be like “&Comment Button”.
- Now, again right-click on the button and select the “Image and Text” and after that come back to the visual basic editor.
Now you can convert a line into a comment by using the shortcut key Alt + C. And if you want to create a shortcut key for the uncomment button you can simply use the above steps to add the uncomment button to the toolbar and the shortcut key for it will be Alt + U.
Change the Format of the Comment
VBA gives you an option to change the format of the comment if you want to do so. From the Tools ➜ Options ➜ Editor Format, click on the comment text.
As you can see, I have changed the color of the comment text from green to blue. Now all the comments which I have in the code window are in blue color.