VBA Comment Block

What is Excel VBA Comment Block of Code?

VBA is a programming language that executes written instructions line by line. However, as a part of debugging the code, we need some instructions. Hence, we add comments inside the sub-procedure, which helps us to debug the code quite easily. It makes the code more informative, understandable, readable, and organized for new users.

Often, we don’t want some sort of code anymore in the procedure but removing them makes the job difficult if we need them later; hence, we can comment the whole block of code so VBA will not treat that commented block of code as a comment, not as VBA code.

We can comment on the VBA code block by adding apostrophe (‘) at the start of the code to convert the code to comment.

For example, look at the following code.

Sub Comment()
Range(“A1”).Select
‘After selecting the cell, we will insert a value “Excel VBA” to selected cell
Selection.Value = “Excel VBA”
End Sub

Whatever is seen in green is a comment. We can see an apostrophe (‘) added at the start of the line. Hence, that line is converted into a comment. In simple terms, any line starting with an apostrophe (‘) is considered a comment.

Key Takeaways
  • We can turn the code into a comment by adding the apostrophe before the start of the code.
  • VBA Editor turns the code from black to green as soon we convert the code to a comment.
  • The VBA Editor window ignores all the commented lines and does not count them as VBA code.
  • We can add comments using apostrophes, the toolbar, and the REM keyword.
  • We have the Comment Block and Uncomment Block shortcuts available under the toolbar. We can add comments anywhere in the module.

How to Comment on Block of VBA Code?

Let us show you a step-by-step approach to comment on a block of code in VBA.

Follow the steps listed below to VBA comment block code.

  1. Choose the macro sub-procedure where you want to add the VBA code.


    VBA Comment - Step 1

  2. Choose the line you want to comment on and place your cursor at the start of the line. For example, assume we must make the second line a comment. We place a cursor at the beginning of the code.


    VBA Comment - Step 2

  3. Once the cursor is placed at the start of the code, enter the apostrophe (‘).


    VBA Comment - Step 3

  4. To complete the VBA Comment Block, click somewhere else and see the code immediately turning green.


    VBA Comment - Step 4

    This green-colored line code indicates that the text is not a code.


Excel VBA – All in One Courses Bundle (35+ Hours of Video Tutorials)

If you want to learn Excel and VBA professionally, then ​Excel VBA All in One Courses Bundle​ (35+ hours) is the perfect solution. Whether you’re a beginner or an experienced user, this bundle covers it all – from Basic Excel to Advanced Excel, Macros, Power Query, and VBA.

Examples

We will show you a different set of examples in the VBA Comment Block. Let’s start with example #1.

Example #1 – Comment Using Apostrophe (‘)

The apostrophe is the most used technique to write a VBA comment block code. We can include a comment in the VBA code by simply entering the apostrophe at the start of the line.

For example, look at the following code.

Sub Comment_Example1()
Dim k As Long
Dim LR As Long
LR = Cells(Rows.Count, 1).End(xlUp).Row
For k = 1 To 10
Cells(k, 1).Value = k
Next k
End Sub

The above code will loop through cells from 1 to 10 and insert the serial number 1 to 10. However, a few lines of code are not used but are written for future use. These include line number 2 and 3.

VBA Comment - Example 1 - two lines

Since we are not using these two lines of code, we can enter the apostrophe at the start of the code.

VBA Comment - Example 1 - Apostrophe

In this way, by simply entering the apostrophe we can convert the code to comment.

Example #2 – Using Toolbar

Adding apostrophe for one or two lines is good to convert the code to comment. However, when we must convert block of codes to comment, we cannot rely on manual entry of an apostrophe. In these scenarios we must explore the toolbar feature available in VBA.

For example, look at the following code.

Sub Comment_Example1()
Dim k As Long
Dim LR As Long
LR = Cells(Rows.Count, 1).End(xlUp).Row
For k = 1 To 10
Cells(k, 1).Value = k
Next k
End Sub

Follow the steps below to make the code a comment using the toolbar feature.

Step 1: Select the required statements from the code. We must select the block of codes that we want to convert to a comment.

VBA Comment - Example 2 - Step 1

Step 2: Click on “View >>> Toolbars >>> Customize.”

VBA Comment - Example 2 - Step 2

Step 3: This will open the Customize Window. From here, Click on Commands >>> Edit, then select Comment Block.

VBA Comment - Example 2 - Step 3

Click on the “Close” button. Now we will have a VBA comment block shortcut key available on the toolbar for quick access.

VBA Comment - Example 2 - Step 3 - comment close

Now after selecting the line of codes that we want to comment on, click on the comment block icon. It will immediately convert the code into comment.

VBA Comment - Example 2 - Step 3 - Output

As seen, for all the selected lines of code, the Comment Block shortcut feature added an apostrophe just before the start of each line.

It is the VBA comment block shortcut to comment large block of codes.

Example #3 – Using the REM Keyword.

The REM keyword is not a popular choice to comment on the code in VBA. Let us show you how we can add a VBA Comment Block using the REM keyword.

Instead of adding the apostrophe (‘), we can add the keyword REM at the beginning of the code. For example, look at the following code.

Sub Comment_Example3()
Range(“A1”).Select
End Sub

In this macro sub-procedure, a simple code selects cell A1 in the active sheet. Just before the code, add the keyword REM and see what happens.

VBA Comment - Example 3 - Rem

When we add the keyword REM, the whole line turns green and is no longer treated as code inside this macro procedure. Hence, any line of code which starts with the word REM will be treated as a comment.

Example #4 – Un-Comment the Commented Lines Using Toolbar

Once we comment on a large code block, we can un-comment them using the toolbar. For that, first, we must enable the uncomment feature from the toolbar like how we enabled the comment feature from the toolbar. Follow the steps listed below to enable the uncomment feature.

Step 1: Click on the “View >>> Toolbars >>> Customize.”

Example 2 - Step 2

Step 2: This will open the Customize Window, and from here, click Commands >>> Edit, then select Comment Block.

Example 4 - Step 2

Step 3: Double-click on “Uncomment Block.” We can see this feature available on the toolbar.

Example 4 - Step 3

Now we can use it and uncomment the commented code. For example, look at the following code.

Step 3 - Output

We have all the lines of code commented. To uncomment these lines, we must first select all the codes that need to be uncommented.

Example 4 - Step 3 - select all

Now click on the uncomment feature.

Example 4 - Step 3 - uncomment

When we click on this uncomment option, all the commented lines will be turned into codes.

Example 4 - Output

Important Things to Note

  • In VBA, the green lines are comments; however, if we fail to add an apostrophe before the comment line, VBA shows a compile error depending on the nature of the code.
  • We must add the REM keyword by adding a space between the REM keyword and the code.
  • An apostrophe can be present inside the code, but if it is present at the beginning of the line of the code, then it will be treated as a comment.
  • The REM keyword is not present in all the versions of Excel VBA. Hence, using the apostrophe only to comment block of code is recommended.

Frequently Asked Questions (FAQs)

1. Can too many comments in block slow down your VBA code?

No, too many comments will not slow down your VBA code. The performance will be as good as before. However, too much of the data can slow down the VBA code.

2. How do I know if a block of code is commented or not in VBA?

We can quickly identify the commented code by any of the following methods.
All the commented codes are green; hence we can easily differentiate between code and comment.
Any code which starts with an apostrophe is a comment.
If the code begins with the keyword REM, it will be a comment.

3. Is there a way to automatically add a Comment Block in VBA when I type a certain keyword?

If you are typing something with the keyword REM, you can automatically convert the code block to a comment.

Download Template

This article must be helpful to understand the VBA Comment Block, with its formula and examples. You can download the template here to use it instantly.

This has been a guide to VBA Comment Block. We learn the procedures to add comment on a block of code with the help of Apostrophe, Toolbar, and Rem Keyword. You can learn more from the following articles –

Reader Interactions

Leave a Reply

Your email address will not be published. Required fields are marked *