Commenting in VBA

Associated Files Download Links

This article will teach you how to comment a single line or multiple blocks of code in the VBA Editor. To learn about how to interact with Excel Cell Comments using VBA read this article: https://www.automateexcel.com/vba/add-or-remove-cell-comments/

 

VBA Comment Single Line

In Excel VBA, there are several ways to comment lines of a code: single quotation (‘), Comment block button in the toolbar or 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:

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:

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.

vba comment 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:

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:

 

VBA Comment Entire Block

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:

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:

 

Keyboard shortcuts for commenting in VBA

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.

You may also like some of this related content...

Advertisements
Automate Excel
Left Menu Icon