How to Add a Comment in a VBA in Excel?

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.