Excel VBA to Comment Multiple Lines (3 Quick Steps)

If you want to comment on multiple lines in a VBA code to make the code easily understandable, this article can help you to do this in 3 simple steps. Generally, when you don’t want to execute a code block but want to keep the code in the Sub Routine, you can convert the code block by converting it into the comment by using Comment Block. So, let’s start learning how to use Excel VBA to Comment Multiple Lines. I will use Microsoft Excel 365 version here.


How to Launch VBA Code Editor

There are various methods by which you can open the VBA editor. The most commonly used method is Developer Tab >> Visual Basic and the simplest way to launch the VBA editor is to press ALT+F11.

Opening the VBA Editor by Using Developer Tab

Then, go to Insert>>Module to create a new module.

Creating New Module

If you don’t have the Developer tab in your Excel workbook, follow the below steps:

  1. First, right-click anywhere on the Tab. Then, click on Customize the Ribbon…

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

  1. Go to Customize Ribbon >> Developer. Next, select it and finally click OK.

Selecting Developer from Customize Ribbon


Excel VBA to Comment Multiple Lines: Do This with These 3 Steps

Multiple line comments, also known as block comments, are often used in VBA code to provide a more detailed explanation of a section of code that spans several lines. They can be particularly useful when documenting the code or providing context to other programmers who may be working on the same project.

Often, you need to write such VBA Code where there are multiple loops or multiple steps in a Sub Routine. If you don’t want to execute a portion in a certain Run and also don’t want to delete the code block as this needs to be used later then converting the multiple lines of the code blocks into comments can work for you. Adding VBA comments in multiple lines can be done with some really simple steps.

This is an image of a code where two For loops are created. The first loop gives the Even Numbers and the second one gives Prime Numbers. Now, if I don’t want to get the Prime Numbers for a particular Run, I can just simply 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 01 to add Comment and Uncomment Block to the Toolbar. In Step 02 and Step 03, using Comment and Uncomment Block is shown.


Step 01: Customize the Toolbar to Add Commands

Firstly, click on View  >> Toolbars  >> Customize.

Clicking on View to Customize Toolbar

Then, 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 02: Utilize Comment Block Command to Add Comment in Multiple Lines

After Step 01, you can see the icon of Excel VBA Comment Block in the Toolbar. Now, select the multiple lines of the code that you want to convert into Comments. Then, click on the Comment Block.

Using Comment Block Toolbar

As a result, the selected lines have converted into a Comment Block.

Inserting Excel VBA Comment in Multiple Lines


Step 03: Utilize Uncomment Block to Remove Comment from Multiple Lines

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

Using Uncomment Block Toolbar

Thus, 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

Sometimes, you need to add a comment in the active cell of the worksheet. To do this copy the code given 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

Now, 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.


Things to Remember

  • Use simple and concise language to ensure that your comments can be easily understood by anyone who reads them.
  • Use appropriate formatting to make your comments easy to read. Use line breaks, indentation, and spacing to break up your comments into logical sections.
  • Provide enough information in your comments to make them meaningful. Explain why the code is written in a certain way or what the code is doing.
  • Make sure your comments are relevant to the code. Avoid commenting on things that are not related to the code, such as personal opinions or unrelated topics.
  • Use a consistent style when writing comments to make them easy to read and understand. This will also help other developers who read your code to understand your comments more easily.

Download Practice Workbook

You may download the following Excel workbook for better understanding and practice it yourself.


Conclusion

In this article, I have explained how to add Excel VBA to comment multiple lines. Moreover, uncommenting the lines is also covered here. In complex VBA codes, adding comments is such an important task. It is really easy to add comments in the VBA code. I hope this article helped you to solve your problem. Please leave a comment if you face any problems regarding this topic.

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