How to Comment Single Line/Block in Excel / VBA

In VBA, you may add single line comments or block (multiple line comments) by using:
Single quote for single comment
Using the menu option for block of comments

Commenting Single Line or Block in VBA

Adding comments to the code is useful in describing the purpose of a line or block of code.

In VBA, you may add single-line comments or block (multiple-line comments) by using:

  • Single quote for a single comment
  • Using the menu option for the block of comments

These comments are not executed by the compiler.

An example of adding single line comment in VBA

In this example, we are adding two single-line comments by using a single quote at the beginning of the line.

Have a look at the code and its image to see how comments look:

Sub comment_ex()

'This program displays the date in day, full month name and year in four digits format

Dim MyDt As Date

MyDt = Date

'Use mmmm for Full Month name and yyyy for Year in four digits

MsgBox (Format(MyDt, "dd mmmm yyyy"))


End Sub

In code editor:

VBA-comment-single-line

You can see, we wrote the purpose of the program in the first line by using a single code at the beginning of the comment.

The second comment is in the context of the code describing its purpose.

Steps to add comment block by menu option

If you need to add multiple lines that you want to be commented then either add a single quote at the beginning of each line as shown above OR

Follow the steps below for making block commenting:

Step1:

We assume you already opened the VBA editor. If not, press Alt+F11 from MS Excel (shortcut to open the VBA editor from Excel).

Step 2:

Now write multiple lines that you want to comment and avoid the compiler to execute.

Select all the lines in that block that you want to comment

Step 3:

Right-click anywhere on the VBA Toolbar and click “Edit” as shown below:

VBA-comment-block-1

Step 4:

Click on “Comment Block” on the window that appeared;

VBA-comment-block-2

This process will make all selected lines commented.

VBA-comment-block-result

Basically, the “Comment Block” button on the toolbar adds a single quote for each line.

Clicking once makes each line commented in that block.

If you want to add more single quotes, keep on clicking the “Comment Block” button as you want to add another single quote.

Uncommenting the block of code

In order to uncomment a line of code or block, click on the button adjacent to “Comment Block” i.e. “Uncomment Block”.

For that, again select the lines in the editor that you want to uncomment and click on the “Uncomment Block” button as shown below:

VBA-uncomment-block

This will remove single quotes from the beginning of each line that you have selected.

Note that, these lines can be proper code that you just don’t want to execute for the time being or instructions of the program as well.