Open In App

How to Add a Comment in a VBA in Excel?

Last Updated : 14 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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. 

No-developer-tab

Follow the steps: 

Step 1: Right-click on any tab in the menu bar. Click on Customize the Ribbon. A dialogue box appears. 

Customizing-the-ribbon

Step 2: In the Mains Tab, check the box Developer and click on Ok.

Checking-box-developer

Step 3: Now the Developer Tab is visible. 

Visible-developer-tab

Step 4: Go to Developer Tab and click on Visual Basic. 

Clicking-developer-tab

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. 

Dialogue-box-opening

Step 6: Write the macro name and click on create. 

Creating-macro-nameSub-macro-created

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. 

Inserting-comment

Step 2: Type an Apostrophe( ‘ ) at the start of a line. 

Typing-apostrophe

Step 3: Write the comment you want. 

Writing-comment

Step 4: Press Enter and you fill find the comment written to be green. 

Comment-shown-in-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. 

Reaching-toolbar

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. 

Editing-in-toolbarExtended-toolbar

Step 3: Select the text you want to comment on and click on Comment Block. 

Selecting-comment

The entire selected text got commented. 

Selected-text-commented

Using Buttons to add a comment

Step 1: Go to Toolbar and right-click on it. A menu appears. 

Clicking-on-toolbar

Step 2: Click on Customize and a dialogue box appears. 

Dialogue-box-appears

Step 3: Go to edit in the left-side scrollable list. 

Editing-in-customize

Step 4: Find Comment Block and Uncomment Block in the right-side scrollable list.

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

Clicking-comment-block

Step 6: Click on Uncomment Block and drag it to the menu bar. It will look like a button in the menu bar. 

Clicking-uncomment-block

Step 7: With the dialogue box opened. Go to the comment block and right-click on it. A menu appears.

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. 

Adding-character

Step 9: Again, right-click on the Comment Block and select Image and Text.

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

Adding-character-in-nameEditing-image-and-text

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. 

Editing-text

Step 12: To comment on the written line. You can click Alt + C.

Commenting-on-written-line

Step 13: To uncomment a line, you can press Alt + U. 

Uncommenting-a-line

Use Rem to Comment

At the start of the comment use the keyword Rem to comment on a line. 

Commenting-a-line

Formatting Comments 

Step 1: Go to Tools Tab, and right-click on it. 

clicking-tools-tab

Step 2: A menu appears, and click on Options… A dialogue box appears. 

Clicking-options

Step 3: Go to Editor Format.

Opening-editor-format

Step 4: Select the Comment Text from the left scrollable list. 

Selecting-comment-text

Step 5: You can change the color of the comment by selecting Foreground. For example, red. Click Ok

Changing-color

Step 6: Now, all comments will have a font color of red. 

Red-color-added



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads