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.
Go to Insert>>Module to create a new module.
If you don’t have the Developer tab in your Excel workbook,
- Right-click anywhere on the Tab. Click on Customize the Ribbon…
- Go to Customize Ribbon >> Developer. Select it and click OK.
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.
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.
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.
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.
The selected lines have been converted into a Comment Block.
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.
The lines have become part of the code again and will be executed if you Run this.
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:
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.
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.
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!