When working with VBA coding in Excel, you can easily add comments while writing the code.
Comments in VBA could be really useful for beginners, where you can add a comment to a line of code (or a block of code) that explains what it does. So the next time you come back to the code, you’ll not be completely lost and would have some context because of the comments.
Even for advanced Excel VBA programmers, once the code starts to get beyond a few lines, it’s a good idea to add context using comments (especially if there is a chance that someone else may have to work on the code in the future)
And since it’s a comment, VBA ignores it while executing the code.
In this short Excel tutorial, I will cover how to add comments in VBA and all the best practices around it.
Adding Comments in VBA in Excel
To add a comment in VBA, simply add an apostrophe sign before the line that you want to be marked as a comment.
Anything after the apostrophe sign in that line would be considered a comment and VBA would turn it into green color (to visually differentiate it from regular code)
There are two ways you can add a comment in VBA:
- Have a comment in a separate line, where this line starts with an apostrophe and then has the comment text after it
- Have a comment as a part of the regular code line, where after the code you have a space character followed by an apostrophe, and then the comment(as shown below)
While I’ve seen both of these being used by the VBA programmers, I prefer the first method where a comment has a separate line altogether.
Another (old school) method of adding a comment is to have the word ‘Rem’ followed by the comment.
Here Rem is short for Remark.
Rem was used in the days of BASIC and has been kept in the current versions of VBA. While it’s good to know that it exists, I recommend you only use the apostrophe method while adding comments in VBA.
Converting a Line of Code (or Block of Code) into Comment
Sometimes, you may have a need to convert an existing line of code (or a block of code) into comments.
Programmers often do this when they’re working on a code and they want to quickly try out something else, while still keeping the already written code.
So you can quickly comment out a line, try a new one, and if you want to get the earlier code back, just remove the apostrophe and convert that comment back into a normal line of code.
For a line (or even a few lines), it’s best to manually add the apostrophe before these lines.
But if you have a large block of code, use the below steps to add the option to convert an entire block of code into a comment:
- Click the View tab
- Go to the Toolbar option.
- When you hover your cursor over it, you’ll see more options
- Click on the Edit option. This will make the edit toolbar appear somewhere on your screen.
- Drag the Edit toolbars towards the toolbar area so that it would dock itself there (in case it’s not docked already)
- Select the block of code that you want to comment out
- Click on the ‘Comment Block’ option in the toolbar
The above steps would instantly convert a block of code into comments by adding an apostrophe in front of every line in that code.
In case you want to remove the comment and convert it back into regular code lines, select that block of code again and click on the ‘Uncomment block’ option in the Edit toolbar
Changing the Color of the Comment in Excel VBA
While VB doesn’t allow a lot of formatting, it does allow you to change the color of the comment if you want to.
One of my VBA course students emailed me and told me that the ability to change the color of comments in VBA was really useful for people suffering from color blindness.
Below are the steps to change the color of the comment in Excel VBA:
- Open the Visual Basic Editor
- Click the Tools option in the menu
- Click on Options
- In the Options dialog box, click on the ‘Editor Format’ tab
- In the Code colors options, select Comment Text
- Change the Foreground and/or the background color
- Close the dialog box
When you change the comment color, it would also change the color for all the existing comments in your code.
Some Best Practices when Working with Comments in VBA
Here are some of the best practices to keep in mind when using comments in the VBA code.
- Keep the comment meaningful and add context. When adding a comment, consider what would be helpful for a new user who has never seen this code and is trying to make sense of it.
- Avoid excessive commenting as it would make your code look a bit cluttered. While it’s alright to add more comments when you are a beginner, as you gain more experience in VBA coding, you would anyway not need to add a lot of comments.
- For every new Subroutine or Function, it’s a good idea to add a comment that explains what it does.
- When working with complex code, it’s a good idea to add comments before conditions and loops, so that it’s easier for you to get a handle on what you had done when you revisit the code (or when someone else goes through the code)
In this tutorial, I covered how you can add comments in VBA and some best practices to use it.
I hope you found this tutorial useful.
Other Excel tutorials you may also like:
- How to Insert / Delete Comments in Excel (including Shortcuts)
- How to Print Comments in Excel
- Get a List of All the Comments in a Worksheet in Excel
- Working with Worksheets using Excel VBA
- Using Workbook Object in Excel VBA (Open, Close, Save, Set)
- Useful Excel Macro Examples for VBA Beginners (Ready-to-use)
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.
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
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.
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.
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).
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.
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.
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.
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.
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:
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.
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.
Return to VBA Code Examples
This article will teach you how to comment a single line or multiple blocks of code in the VBA Editor. Instead, if you want to learn about how to interact with Excel Cell Comments using VBA read that article.
In Excel VBA, there are several ways to comment lines of a code:
- Single quotation (‘)
- Comment block button in the toolbar
- Adding the Rem keyword.
The easiest way to comment a line of a code is putting a single quotation at the beginning of the line:
'Sheet1.Range("A1").Value = "Test"
Notice that in VBA, comments are always displayed as green text.
As you can see in the example, we put a single quotation at the beginning of the first line in the procedure and commented it. If a quotation is put at the beginning of the line, the whole line is commented and will be skipped during execution of the code.
You can also comment part of the code if you put a single quotation somewhere in the line.
In that case code after a quotation will be skipped:
Sheet1.Range("A1").Value = "Test" 'The example of partial line commenting
Now we commented only part of the line. This is a good way for writing inline comments in a code.
The second way for commenting a line in a code is using the standard VBA button for comment in the toolbar. In order to display this button, you need to add it: View -> Toolbars -> Edit. Now you can see two buttons in the toolbar: Comment block and Uncomment block.
Simply highlight your desired line(s) of code and click one of the buttons. This will comment/uncomment entire lines. Please note that this method will not allow you to add a comment to the end of a line of code.
You can also use the keyword Rem. In order to comment a line, you need to put this keyword at the beginning of a line:
Rem Sheet1.Range("A1").Value = "Test"
Similarly to comment button, the Rem keyword allows you to comment just a whole line of a code, which means that you can put it only at the beginning of a line:
Apart from commenting a single line, we often need to comment multiple lines, a block of code. In order to do this, we can the same standard button Comment Block in the toolbar which we used for commenting a single line. First, we need to select all the lines that we want to comment and then click on the button:
Private Sub CommentEntireBlock()
' Sheet1.Range("A1").Value = "Test"
' If Sheet1.Range("A1") = "Test" Then
' MsgBox "The value of A1 cell is: Test"
' End If
End Sub
As a result, the whole block of code is commented.
Similarly, we can uncomment a block, by clicking on the Uncomment Block button in the toolbar:
Private Sub CommentEntireBlock()
Sheet1.Range("A1").Value = "Test"
If Sheet1.Range("A1") = "Test" Then
MsgBox "The value of A1 cell is: Test"
End If
End Sub
To enable keyboard shortcuts for commenting:
- Right-click somewhere on empty space in the toolbar.
- Choose Customize option and select the Edit under the categories.
- Find Comment Block in the Commands and drag and drop it next to the existing icons in the toolbar.
- Now you can see the newly added button in the toolbar
- Click on the Modify Selection and check option Image and Text.
- Click again on the Modify Selection and under Name add an ampersand (&) at the beginning of the name, so the name of the button is “&Comment Block”.
Now you can select a single line or a block of code and press Alt+C on your keyboard to comment.
To enable the same option for uncommenting a code, you can repeat the whole process for Uncomment Block command. The shortcut for uncommenting is ALT+U.
VBA Coding Made Easy
Stop searching for VBA code online. Learn more about AutoMacro — A VBA Code Builder that allows beginners to code procedures from scratch with minimal coding knowledge and with many time-saving features for all users!
Learn More!
VBA Macro is for developers. Macro is a piece of code written in VBA. VBA is Microsoft’s programming language and it stands for Visual Basic for Applications. Let’s see how to set up our VBA Macro and how to add comments in a VBA in Excel.
VBA Macro
Excel is a very advanced tool that contains thousands of functionalities, but VBA Macro comes into existence when we have to do a repeated task. The complex repeated tasks can be automated with the help of VBA Macro.
Initial Set-Up
Go to Developer Tab. We will see that by default developer tab is not present in the menu bar.
Follow the steps:
Step 1: Right-click on any tab in the menu bar. Click on Customize the Ribbon. A dialogue box appears.
Step 2: In the Mains Tab, check the box Developer and click on Ok.
Step 3: Now the Developer Tab is visible.
Step 4: Go to Developer Tab and click on Visual Basic.
Step 5: Now, the VBA tab is opened. Click on Tools in the menu bar and then click on Macros. A dialogue box is open.
Step 6: Write the macro name and click on create.
A Macro is created.
VBA Comments
Comments are the lines in the code that are ignored while executing the code. These are represented as green text in the code. The comments help describe the written code. Knowing the correct use of comments is very important because while working with long and complex code, comments help us identify which part of code does what. It is very helpful for development purposes.
Adding Single Line comment in VBA
Step 1: Click on the line where you want to insert a comment.
Step 2: Type an Apostrophe( ‘ ) at the start of a line.
Step 3: Write the comment you want.
Step 4: Press Enter and you fill find the comment written to be green.
Adding Multi-Line comment in VBA
We can add comments in multiple lines. We use multi-line comments when we have to add points in our description or the description is long.
Step 1: Keep your cursor on the Tool Bar.
Step 2: Right-click on the Tool Bar and click on edit. An extended Tool Bar appears. Drag and place it in the already existing Tool Bar.
Step 3: Select the text you want to comment on and click on Comment Block.
The entire selected text got commented.
Using Buttons to add a comment
Step 1: Go to Toolbar and right-click on it. A menu appears.
Step 2: Click on Customize and a dialogue box appears.
Step 3: Go to edit in the left-side scrollable list.
Step 4: Find Comment Block and Uncomment Block in the right-side scrollable list.
Step 5: Click on Comment Block and drag it to the menu bar. It will look like a button in the menu bar.
Step 6: Click on Uncomment Block and drag it to the menu bar. It will look like a button in the menu bar.
Step 7: With the dialogue box opened. Go to the comment block and right-click on it. A menu appears.
Step 8: Click inside the Name and add a character & at the starting of Comment Block. Then click somewhere outside the appeared menu.
Step 9: Again, right-click on the Comment Block and select Image and Text.
Step 10: Repeat steps 7, 8, 9 for Uncomment Block i.e. right-click on the Uncomment Block and add & in the Name. Also, select the Image and Text in the appeared menu. At last, close the dialogue box.
Step 11: A shortcut for comment and uncomment has been created in the VBA code editor. To comment on a line the shortcut is Alt + C and to uncomment a line the shortcut is Alt + U. You can also use the Comment Block and Uncomment Block buttons to comment on a line. Enter the text you want to comment on.
Step 12: To comment on the written line. You can click Alt + C.
Step 13: To uncomment a line, you can press Alt + U.
Use Rem to Comment
At the start of the comment use the keyword Rem to comment on a line.
Formatting Comments
Step 1: Go to Tools Tab, and right-click on it.
Step 2: A menu appears, and click on Options… A dialogue box appears.
Step 3: Go to Editor Format.
Step 4: Select the Comment Text from the left scrollable list.
Step 5: You can change the color of the comment by selecting Foreground. For example, red. Click Ok.
Step 6: Now, all comments will have a font color of red.
Excel VBA Comment
Commenting a line in VBA is a very simple yet useful option from a programmers perspective. Sometimes, we often struggle to comment on a line irrespective of how good we are in programming. Comments can be used to pass the instructions to the user about the code or function or some statements in a laymen language. It makes users/programmers life easy because these codes can be a good piece of information about what that code is about and how it can be used, or how some of the statements/functions are associated with each other.
We will see different methods to comment on a single line and a group of lines in this article.
How to Comment Using VBA?
Let’s see the examples of Comment in Excel VBA.
You can download this VBA Comment Excel Template here – VBA Comment Excel Template
Example #1 – Commenting Single Line Using Apostrophe/Single Quote (‘)
Any text followed by an apostrophe is considered as a comment in VBA, and it will be opted out of the execution of the main code.
Step 1: Insert a new module in your Visual Basic Editor (VBE).
Step 2: Define a sub-procedure in the newly inserted module to create a macro in VBE.
Code:
Sub CommentEx1() End Sub
Step 3: Write any piece of text within the created macro. I will write the line “This is a comment to be added”.
Code:
Sub CommentEx1() This is comment to be added End Sub
Step 4: Before the first word of this line, insert an apostrophe (Single quotation mark) to make this line a comment.
Code:
Sub CommentEx1() 'This is comment to be added End Sub
Step 5: As soon as you insert an apostrophe before the first word of your line, it becomes green. Which means that the line is commented out. See the below screenshot:
Code:
Sub CommentEx1() 'This is comment to be added End Sub
Step 6: You can add comment anywhere in the middle of your code using an apostrophe. See the example screenshot below:
Code:
Sub CommentEx1() MsgBox "This is comment to be added" 'This is comment to be added End Sub
If you can see this piece of code, under MsgBox, there is a text to be popped out in the message box, and then there is the line of comment, which says it is a line of comment to be added.
Step 7: Hit F5 or Run button to run the code manually and see the output as shown in the screenshot below:
Example #2 – Commenting Single Line Using REM Keyword in VBA
This is the least suggested method because it has its own pitfalls. Let’s see how it works:
Step 1: Insert a new Module in VBE.
Step 2: Define a sub-procedure to create a macro.
Code:
Sub CommentEx2() End Sub
Step 3: Add a line of text which you wanted to comment on out of the code. I will take the Line “This is a comment line”.
Code:
Sub CommentEx2() This is a comment line End Sub
Step 4: Use the keyword REM at the start of your line, and it will be converted into a comment.
Code:
Sub CommentEx2() Rem This is a comment line End Sub
There are some pitfalls of its own for REM.
- This keyword doesn’t allow you to add comment anywhere in the middle of your code.
- It always needs at least a single space between REM and your line of text for proper evaluation of the comment.
Example #3 – Commenting a Single Line – Comment/Uncomment
Using this method, you don’t need to type apostrophe at the start of your text; all you need is to click on the comment button, which is there at the top of the VBA panel.
Step 1: Insert a new module and define a new sub-procedure to create a macro.
Code:
Sub CommentEx3() End Sub
Step 2: Add a comment line which you wanted to be skipped from your code execution.
Code:
Sub CommentEx3() This is a line to be commented End Sub
Step 3: Now, put the cursor of your mouse anywhere on the line which you want to comment out.
Code:
Sub CommentEx3() This is a line to be commented End Sub
Step 4: Click on View under the uppermost panel of VBE.
Step 5: Navigate towards Toolbars and select Edit
Step 6: You’ll see a new toggle bar popping up in VBE as below, where two buttons can be seen for Comment and Uncomment.
Step 7: Click on the Comment button and see the line of text being commented.
These are few methods using which we can comment on a single line in VBA. Let’s see how we can comment on a group of lines in VBA.
Example #4 – Commenting a Group of Lines in VBA
Unfortunately, there is no single keyword to comment on a group of lines in VBA. You can use the apostrophe to comment out each line at a time. Which doesn’t seem to be a proficient way to do so, right? I mean, think on a bigger picture, a code where you have thousands of comments. You surely would not like to comment on them one by one using apostrophe, are you?
There is, however, a method to do so in a single shot.
Step 1: Suppose you have multiple lines of comments as shown below:
Code:
Sub CommentEx4() Hello VBA Learner! Hope you all are doing great My Name Is Lalit End Sub
Now, these three lines, I want to make a comment. You already have seen the edit toggle bar, which has Comment/Uncomment button with it.
Step 2: Select all the lines which you wanted to comment out and click on Comment Button on the Edit toggle bar. You’ll see the output as shown below:
This block of lines is being commented on. You also can use the uncomment button to uncomment the selected range of lines.
This is it from this article; let’s wrap the things up using some things to remember.
Things to Remember
- A single line can be commented out using apostrophe or REM Keyword. Placing them at the start of the line.
- For a block of the text line, the Comment/Uncomment button works handy to comment/uncomment a block of lines.
- An apostrophe can be used anywhere in the middle of the code. In that case, the line next to the apostrophe will be considered as a comment.
- REM Can not be used anywhere in the line. Using this keyword, you can only comment out the text at the start of a line.
Recommended Articles
This is a guide to VBA Comment. Here we discuss how to use Comment in Excel VBA along with few practical examples and a downloadable excel template. You can also go through our other suggested articles –
- VBA While Loop
- VBA Remove Duplicates
- VBA Data Types
- VBA Sleep