Dear Friends,

This is a very simple yet useful article, especially for beginners. Sometimes even intermediate level users also does not know about all these options to comment a line or group of lines in Excel VBA.

Like every other programming languages, VBA too has an option to comment a line or group of lines in your code.
For those who does not know – what is comment in a Programming:

Commenting a line or group of lines in any programming language means – to instruct the execution control or compiler to skip those lines from compilation or execution.

Before I tell you how to comment a line in Excel VBA, let me explain you – use of it

What is the use of comments in Excel VBA Programming

As mentioned above these comments are something which are not compiled or executed, programmer can write anything in any simple human language to give a brief description about a function, or a variable, or a statement etc.
This is makes programmers life easier from maintenance perspective. By reading those brief description about the code, any programmer can have a basic idea what that function or statement or variable etc. does.

It is not MANDATORY but a good programming practice to provide proper comments so that any other programmer can easily understand the code easily.

How to comment a single line in VBA code

Basically there are 3 ways to do so.

Method 1: Using Single Quote (‘)

This is one of the simplest method to comment a single line in VBA programming.
To comment a line, you can simply type single quote (‘) at the beginning of that line. Whole line will turn in to green text. That’s all !
Now that line would neither be compiled nor executed.
Comment-line-in-VBA

Note:

Single quote can be put somewhere in the middle of a line as well. In such case, rest of the text after the single quote (‘) will be treated as comment.
Refer the below image

Comment Line in VBA

Comment Line in VBA

Method 2: Using REM Keyword in Excel VBA

This is also a simple one but I do not prefer this. You can see the reason below in note section.

To comment any line in VBA you can start that line with the keyword REM So in this method, instead of single quote (apostrophe) you can type Rem keyword.
Rem keyword in Excel VBA

Note:

Following are the limitation using this Rem keyword
1. There should be at least one space between start of your comment and Rem keyword.
2. Like single quote, Rem keyword can not be used in the middle of a line to comment rest of the line. Rem keyword must always be the first word to start with.

I think above two reason are more than enough for me to not to like it :).

Method 3: VBE built-in Option – Comment/Uncomment Block

This is same method as first one, but here you do not need to type Single quote by yourself, rather you can follow the below steps:
1. Place your mouse cursor anywhere on the line which you want to comment
2. Press on “Comment” button as shown in below video

Comment-Uncomment-VBA

Important:

Using this method you also get an option to press “Uncomment” button as well as shown in the above gif video.

How to comment or un-comment multiple lines together

Unfortunately in VBA there is no short cut key to do so. But don’t worry, method 3 is very useful in this case.
You can use the same buttons Comment Block and Un-comment Block to do this.
Simply select all the lines you want to comment or un comment it and press the relevant button as shown below:

1# How to comment group of lines (Block) in VBA

Step 1: Select all the lines (block)
Step 2: Press the Comment Block as shown in above picture
Comment-Uncomment-VBA-Method

2# How to Un-comment group of lines (Block) in VBA

Step 1: Select all the commented lines (block)
Step 2: Press the Uncomment Block as shown in above picture
Comment-Uncomment-VBA-Method

What if Comment Block and Uncomment Block Button not visible

Follow the below steps:
Step 1: Go to View –> Toolbars –> Edit

VBE - View Edit Option

VBE – View Edit Option


Step 2: Check the Edit option. You will see Edit tool bar where you can find these two options along with others.
Comment-uncomment-button

Comment-uncomment-button

cover3d_0-89071700_1484285537__1_

Join over 10, 000+ Excel VBA Enthusiasts & get this FREE e-Book Now!