EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home VBA VBA Resources VBA Message Functions VBA Comment
 

VBA Comment

Madhuri Thakur
Article byMadhuri Thakur

Excel VBA Comment

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.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

How to Comment Using VBA?

Let’s see the examples of Comment in Excel VBA.

You can download this VBA Comment Excel Template here – VBA Comment Excel Template

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).

VBA Command Module

Step 2: Define a sub-procedure in the newly inserted module to create a macro in VBE.

Code:

Sub CommentEx1()

End Sub

VBA Comment Example 1

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

VBA Comment Example 1.1

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

VBA Example 1.3

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

VBA Example 1.4

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

 Example 1.5

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:

VBA Comment

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.

VBA Module 2

Step 2: Define a sub-procedure to create a macro.

Code:

Sub CommentEx2()

End Sub

VBA Example 2.1

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

VBA Example 2.2

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

excel vba comment Example 2.3

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

Example 3.1

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

Example 3.2

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

 Example 3.3

Step 4: Click on View under the uppermost panel of VBE.

ToolBar

Step 5: Navigate towards Toolbars and select Edit

CMT Toolbar - 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.

Comment uncomment

Step 7: Click on the Comment button and see the line of text being commented.

Comment 1

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

excel vba comment Example 4.1

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:

VBA Comment 2

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 –

  1. VBA While Loop
  2. VBA Remove Duplicates
  3. VBA Data Types
  4. VBA Sleep

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Web Development & many more.

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA

Download VBA Comment Excel Template

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - ENROLL NOW