Excel VBA to Comment Multiple Lines (3 Quick Steps)

How to Launch VBA Code Editor

The most commonly used method to open the VBA editor is Developer Tab >> Visual Basic or to press ALT+F11.

Opening the VBA Editor by Using Developer Tab

Go to Insert>>Module to create a new module.

Creating New Module

If you don’t have the Developer tab in your Excel workbook,

  1. Right-click anywhere on the Tab. Click on Customize the Ribbon…

Right-clicking Anywhere on the Tab Section to Find Customize the Ribbon

  1. Go to Customize Ribbon >> Developer. Select it and click OK.

Selecting Developer from Customize Ribbon


Excel VBA to Comment Multiple Lines

The image below is of the code where two For loops are created. The first loop gives the Even Numbers and the second one gives Prime Numbers. Now, if we don’t want to get the Prime Numbers for a particular Run, we can Comment Block the second For loop.

Sample Code to Add Comment on Multiple Lines in Excel VBA

If you don’t have Comment and Uncomment Block in your code editor’s Toolbar, follow Step 1 to add Comment and Uncomment Block to the Toolbar. Step 2 and Step 3 shows how to use Comment and Uncomment Block.


Step 1 – Customize the Toolbar to Add Commands

Click on View  >> Toolbars  >> Customize.

Clicking on View to Customize Toolbar

From the Customize dialog box, click on Commands. From Categories, select the Edit option. In Commands, scroll down the button and find Comment Block and Uncomment Block. Drag the Comment Block and the Uncomment Block to the VBA code editor’s Toolbar and Close the dialog box.

Adding Comment and Uncomment Block as Commands


Step 2 – Utilize Comment Block Command to Add Comment in Multiple Lines

After Step 1, you can see the icon of Excel VBA Comment Block in the Toolbar. Select the multiple lines of the code that you want to convert into Comments. Click on the Comment Block.

Using Comment Block Toolbar

The selected lines have been converted into a Comment Block.

Inserting Excel VBA Comment in Multiple Lines


Step 3 – Utilize Uncomment Block to Remove Comment from Multiple Lines

After converting it into a Comment, you can also Uncomment these lines. Select the lines where you want to use the Uncomment Block. Click on the Uncomment Block Icon from the Toolbar.

Using Uncomment Block Toolbar

The lines have become part of the code again and will be executed if you Run this.

Uncommenting Multiple Lines in Excel VBA


How to Add Single Line Comment in VBA Code Using Single Quotation (‘)

In VBA, you can add a single-line comment using an apostrophe (‘) character. This will comment out everything on the same line after the apostrophe. Here’s an example:

Using Single Quotation to Add Comment in Single Line


How to Use the REM Keyword to Add Comment in a Single-Line VBA Code

To add a comment, start a new line in your code and type the REM keyword followed by a space and then your comment. Follow the image given below.

Adding Single Line Comment in VBA Excel by Using REM Keyword


How to Add Comment in an Active Cell with VBA

If you need to add a comment in the active cell of the worksheet, enter the code below:

Sub Add_Comment()
    Dim myRange As Range
    Dim myComment As String
    myComment = "This is the active cell."
        Set myRange = ActiveCell
    myRange.AddComment
    myRange.comment.Text Text:=myComment
End Sub

Run the code and you will see the comment in the active cell. In this example, the B4 cell was active and so the comment is added to this cell.

VBA Code to Add Comment in an Active Cell

Code Breakdown

Dim myRange As Range
Dim myComment As String

It declares a variable named “myRange” of type “Range” to hold a reference to a range of cells. It also declares another variable named “myComment” of type “String” to hold the text of the comment to be added.

myComment = "This is the active cell."

The text “This is the active cell.” is assigned to the “myComment” variable.

Set myRange = ActiveCell

The “ActiveCell” property is used to get a reference to the currently selected cell, which is then assigned to the “myRange” variable using the “Set” keyword.

AddComment
    myRange.comment.Text Text:=myComment
End Sub

The “AddComment” method of the “Range” object is called on the “myRange” variable to add a comment box to the cell. The “Text” property of the “Comment” object is then used to set the text of the comment to the value of the “myComment” variable.


Download Practice Workbook


 

Get FREE Advanced Excel Exercises with Solutions!

Tags:

Mahfuza Anika Era
Mahfuza Anika Era

Mahfuza Anika Era graduated from the Bangladesh University of Engineering and Technology in Civil Engineering. She has been with ExcelDemy for almost a year, where he has written nearly 30 articles and reviewed many. She has also worked on the ExcelDemy Forum and solved 50+ user problems. Currently, she is working as a team leader for ExcelDemy. Her role is to guide his team to write reader-friendly content. Her interests are Advanced Excel, Data Analysis, Charts & Dashboards,... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo