Excel VBA Comment Block of Code

You are free to use this image on you website, templates, etc., Please provide us with an attribution linkHow to Provide Attribution?Article Link to be HyperlinkedFor eg:Source: VBA Comment Block (wallstreetmojo.com)

How to Comment on Block of VBA Code?

Example #1 – Comment using Apostrophe

We wish to comment on a single line/statement/block in a VBA code. We must configure the Visual Basic Editor (VBE) to do this.

The Visual Basic Editor can be accessed as follows:

First, go to the Excel Developer tabExcel Developer TabEnabling the developer tab in excel can help the user perform various functions for VBA, Macros and Add-ins like importing and exporting XML, designing forms, etc. This tab is disabled by default on excel; thus, the user needs to enable it first from the options menu.read more, click “Visual Basic Editor,” or press Alt+F11 to open the “Visual Basic Editor” window.

In doing this, a window opens as follows:

Right-click on the workbook name in the “Project-VBAProject” pane and then click on “Insert” -> “Module” as follows.

Now, we can write our code or procedure in this module:

Code:

Sub macro()

‘This is a Comment

End Sub

So, we can see in the above screenshot that when writing this code in the module, when we put or insert an apostrophe before a statement/line, that statement turns into green text and is considered a comment. So, we see that when we wish to comment on a single line, it can precede us with an apostrophe.

We can also use this method to comment on multiple lines by putting an apostrophe before each line as follows:

Example #2 – Using Toolbar

Suppose we wish to skip over and comment on an entire block of code or multiple statements of the code. In such a case, using an apostrophe before each statement would be tedious and time-taking when we have so many statements to comment on. So to do this, there is a built-in option of “Comment/Uncomment Block” in VBA,  initially hidden in the toolbar, and we can use it as follows:

Select the statements in the macro/procedure that must comment below.

It will generate or open a “Customize” pop-up window. Now, click on “Commands” -> “Edit” and then click on “Comment Block” and drag it to the toolbar.

With this, we now have the “Comment Block” icon on the toolbar for easy access.

Now, click on the “Comment Block” from the toolbar as follows:

In doing so, the highlighted statements/lines would now be commented and turn out to be green in color as below:

Sub CommentLines()

‘MsgBox “First Comment Line” ‘MsgBox “Second Comment Line” ‘MsgBox “Third Comment Line”

End Sub

So, we can see in the above screenshot that the green statements will not execute by the macro and will only be treated as comments blocks.

Example #3 – Using REM Keyword

Another method we can use to make a statement/line as a comment is adding the keyword ‘REM’ before it.

Let us see below how this works:

We can see in the screenshot below that when we add the keyword “REM” before the statement: “This is a comment,” it turns out to be green and hence a comment.

Now, let us see how we can use this keyword to comment on multiple lines in the below screenshot.

Sub CommentUsingRem()

Rem This is a Comment Rem This is a Comment Rem This is a Comment

End Sub

So, we can see that apart from using apostrophes and “Comment Block,” the keyword we can also use “REM” to comment statements of code or procedure. However, using the keyword “REM” has some limitations:

  • Space is mandatory between the keyword “REM”and the start of the statement.It always has to be the first word to start with and cannot be used somewhere in the middle of a line/statement to comment on the rest of the line.

Example #4 – UnComment the Commented lines using Toolbar

Just the way we can comment a block of lines at one go, we can also uncomment the commented lines using the VBE built-in “Uncomment Block” option in the same way as follows:

Select the commented statements in the macro/procedure that we require to uncomment as below:

Now, select View -> Toolbars -> Customize.

It will generate or open a “Customize” pop-up window. Now, click on Commands -> Edit, and then click on Uncomment Block and drag it to the toolbar as follows:

With this, we now have the “Uncomment Block” icon on the toolbar for easy access.

Now click on the “Uncomment Block” from the toolbar as follows:

For doing that, the highlighted statements that commented would now turn into executable statements of the code or procedure and change in color from green to black again as below:

Sub UncommentedLines()

MsgBox “First Comment Line” MsgBox “Second Comment Line” MsgBox “Third Comment Line”

End Sub

So, these statements are no longer comments.

Things to Remember

  • Comments are brief explanatory statements that we can use to describe the procedures.Commenting can be useful in debugging the codes.Any statement in the VBA codeStatement In The VBA CodeVBA code refers to a set of instructions written by the user in the Visual Basic Applications programming language on a Visual Basic Editor (VBE) to perform a specific task.read more that follows an apostrophe is considered a comment.As a good programming practice, comments can be used before each code section or variable declarations and functions to describe their purpose.The VBA EditorThe VBA EditorThe Visual Basic for Applications Editor is a scripting interface. These scripts are primarily responsible for the creation and execution of macros in Microsoft software.read more makes the statement’s font color green to indicate that it is a comment.The compiler ignores the statement following an apostrophe until the end of the line, unless the apostrophe is present in a string.An apostrophe can even be present somewhere in the middle of a line. Text after the apostrophe will be treated as a comment in that case.

The following screenshot illustrates this:

This article has been a guide to VBA Comment Block. Here, we learn three ways to comment blocks of VBA codes using 1) Apostrophe, 2) Toolbar, 3) REM keyword, practical examples and a downloadable Excel template. Below you can find some useful Excel VBA articles: –

  • VBA Workbook OpenVBA RegExVBA Workbook Object