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: 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.

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: 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: 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. 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)