Excel VBA Comment
Commenting a line in VBA is a very simple yet useful option from a programmers perspective. Sometimes, we often struggle to comment on a line irrespective of how good we are in programming. Comments can be used to pass the instructions to the user about the code or function or some statements in a laymen language. It makes users/programmers life easy because these codes can be a good piece of information about what that code is about and how it can be used, or how some of the statements/functions are associated with each other.
We will see different methods to comment on a single line and a group of lines in this article.
How to Comment Using VBA?
Let’s see the examples of Comment in Excel VBA.
Example #1 – Commenting Single Line Using Apostrophe/Single Quote (‘)
Any text followed by an apostrophe is considered as a comment in VBA, and it will be opted out of the execution of the main code.
Step 1: Insert a new module in your Visual Basic Editor (VBE).
Step 2: Define a sub-procedure in the newly inserted module to create a macro in VBE.
Code:
Sub CommentEx1() End Sub
Step 3: Write any piece of text within the created macro. I will write the line “This is a comment to be added”.
Code:
Sub CommentEx1() This is comment to be added End Sub
Step 4: Before the first word of this line, insert an apostrophe (Single quotation mark) to make this line a comment.
Code:
Sub CommentEx1() 'This is comment to be added End Sub
Step 5: As soon as you insert an apostrophe before the first word of your line, it becomes green. Which means that the line is commented out. See the below screenshot:
Code:
Sub CommentEx1() 'This is comment to be added End Sub
Step 6: You can add comment anywhere in the middle of your code using an apostrophe. See the example screenshot below:
Code:
Sub CommentEx1() MsgBox "This is comment to be added" 'This is comment to be added End Sub
If you can see this piece of code, under MsgBox, there is a text to be popped out in the message box, and then there is the line of comment, which says it is a line of comment to be added.
Step 7: Hit F5 or Run button to run the code manually and see the output as shown in the screenshot below:
Example #2 – Commenting Single Line Using REM Keyword in VBA
This is the least suggested method because it has its own pitfalls. Let’s see how it works:
Step 1: Insert a new Module in VBE.
Step 2: Define a sub-procedure to create a macro.
Code:
Sub CommentEx2() End Sub
Step 3: Add a line of text which you wanted to comment on out of the code. I will take the Line “This is a comment line”.
Code:
Sub CommentEx2() This is a comment line End Sub
Step 4: Use the keyword REM at the start of your line, and it will be converted into a comment.
Code:
Sub CommentEx2() Rem This is a comment line End Sub
There are some pitfalls of its own for REM.
- This keyword doesn’t allow you to add comment anywhere in the middle of your code.
- It always needs at least a single space between REM and your line of text for proper evaluation of the comment.
Example #3 – Commenting a Single Line – Comment/Uncomment
Using this method, you don’t need to type apostrophe at the start of your text; all you need is to click on the comment button, which is there at the top of the VBA panel.
Step 1: Insert a new module and define a new sub-procedure to create a macro.
Code:
Sub CommentEx3() End Sub
Step 2: Add a comment line which you wanted to be skipped from your code execution.
Code:
Sub CommentEx3() This is a line to be commented End Sub
Step 3: Now, put the cursor of your mouse anywhere on the line which you want to comment out.
Code:
Sub CommentEx3() This is a line to be commented End Sub
Step 4: Click on View under the uppermost panel of VBE.
Step 5: Navigate towards Toolbars and select Edit
Step 6: You’ll see a new toggle bar popping up in VBE as below, where two buttons can be seen for Comment and Uncomment.
Step 7: Click on the Comment button and see the line of text being commented.
These are few methods using which we can comment on a single line in VBA. Let’s see how we can comment on a group of lines in VBA.
Example #4 – Commenting a Group of Lines in VBA
Unfortunately, there is no single keyword to comment on a group of lines in VBA. You can use the apostrophe to comment out each line at a time. Which doesn’t seem to be a proficient way to do so, right? I mean, think on a bigger picture, a code where you have thousands of comments. You surely would not like to comment on them one by one using apostrophe, are you?
There is, however, a method to do so in a single shot.
Step 1: Suppose you have multiple lines of comments as shown below:
Code:
Sub CommentEx4() Hello VBA Learner! Hope you all are doing great My Name Is Lalit End Sub
Now, these three lines, I want to make a comment. You already have seen the edit toggle bar, which has Comment/Uncomment button with it.
Step 2: Select all the lines which you wanted to comment out and click on Comment Button on the Edit toggle bar. You’ll see the output as shown below:
This block of lines is being commented on. You also can use the uncomment button to uncomment the selected range of lines.
This is it from this article; let’s wrap the things up using some things to remember.
Things to Remember
- A single line can be commented out using apostrophe or REM Keyword. Placing them at the start of the line.
- For a block of the text line, the Comment/Uncomment button works handy to comment/uncomment a block of lines.
- An apostrophe can be used anywhere in the middle of the code. In that case, the line next to the apostrophe will be considered as a comment.
- REM Can not be used anywhere in the line. Using this keyword, you can only comment out the text at the start of a line.
Recommended Articles
This is a guide to VBA Comment. Here we discuss how to use Comment in Excel VBA along with few practical examples and a downloadable excel template. You can also go through our other suggested articles –