How to comment a line in VBA?

In VBA, you can comment a line of code by adding

  1. an apostrophe (single quote – ‘) or
  2. statement ‘REM’

at the beginning of the line. This tells VBA to ignore the line & not execute the code. Here is an how the code looks with few commented lines.

Sub Commenting_a_Line_in_VBA()
    'Here is how a commented line looks like in VBA
    'Almost everyone uses single quote to comment
    'MsgBox "commented line"
    
    'Or use keyword REM
    Rem MsgBox "commented line"

    'Uncommented Code
    MsgBox "commented line"
End Sub

Programmers add comment as, it is a good practice to use comments to explain what the code is doing. This makes it easier for others (or yourself) to understand what the code does.

How to Comment Multiple Lines in VBA Code?

In Excel VBA comment multiple lines of code with this built in option available in toolbar menu:

Follow these steps to enable VBA comment multiple lines of code.

  1. In VBA Menu click View
  2. Choose Toolbars -> Edit
  3. New Icon toolbar will be displayed
  4. Click on highlighted icons to comment or uncomment multiple lines of code.
Excel VBA Comment Multiple Lines of Code using Menu Option
Excel VBA Comment Multiple Lines of Code using Menu Option

Check all the options in the newly displayed toolbar. One of the icon will display tooltip ‘Comment Block’.

In most programming languages, this can be done with a keyboard shortcut. But, there is no keyboard short available for commenting single line of multiple lines in VBA.

Even then, VBA editor has built in menu option to comment multiple number of lines of vba code.

Steps to Comment Multiple lines in VBA

Once the above Toolbar is enabled, You can drag it to quick access space just below the menu. It will always stick there when you open VBA editor next time.

Now, you can follow these easy steps to comment any number of selected block of code in your current VBA Project.

  1. Select multiple lines of code.
  2. Click on the option ‘Comment Block’ from Edit Toolbar.
  3. All the selected lines will be commented.
  4. Click on the icon adjacent to “Comment Block”
  5. Now the comments will be removed.

Similar to commenting, the next option in the toolbar is uncommenting.

This is the reverse of the above option used to uncomment the already commented block of code. This is how You can add comments or comment multiple lines in VBA using simple toolbar option. Hope you like this option. Lets us know what you think of this article in the comments section.

Related Topic that might interest you.

  1. Excel VBA Get Username – 6 Easy Ways using Macro
  2. VCF to Excel Converter – VBA Code to Read vCard File
  3. Excel To VCF Converter – 81,000+ Downloaded – Excel To Vcard App