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 will 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.
This Tutorial Covers:
ToggleAdding Single-Line Comment in VBA
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).
While executing the code, these comments are ignored.
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 add the word REM before the comment (where 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.
Adding Multi-Line Comment in VBA
If you want to add multiple lines of comments in the code, you can have each sentence in a separate line and ensure that each line starts with an apostrophe.
Below is the VBA code where I have three lines of comments:
Sub Hello()
' This is a sample code
' Code Created by Sumit Bansal
' It shows a Message box with the text Hello
MsgBox "Hello"
End Sub
When you put this code in the vb editor, all three lines have an apostrophe before it appears in green color.
If you have a long comment and you want to break it down into multiple lines without converting each line into a separate comment, you can add a space character followed by an underscore sign and then press enter. This way, VBA would know that the comment continues.
Sub Hello()
' Code Created by Sumit Bansal _
It shows a Message box with the text Hello
MsgBox "Hello"
End Sub
Converting a Line of Code (or Block of Code) into a Comment
Sometimes, you may 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 will 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 article, I showed you how to add comments in VBA codes and some best practices while doing it.
I hope you found this article useful. If you have any questions or suggestions for me do let me know in the comments section.
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)