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 Block Comment
 

VBA Block Comment

Madhuri Thakur
Article byMadhuri Thakur

VBA Block Comment

Excel VBA Block Comment

When we write codes in VBA sometimes these codes are meant for other people or other developers. Just a single glance will not provide immediate attention on which function is used for what. This is not the only case for VBA be it any programming language. Commenting on a block of codes which is a fundamental feature of any programming language. Well in VBA, comment block is not enabled by default, we have to do it ourselves.

 

 

Now we know what a block comment means, in this article, we will learn about various ways of commenting on any block of codes in VBA. Comments in VBA will not be executed or compiled with the other codes. Comments are only there to help any other user or oneself while debugging to remember which code is for what. For example, if I write a function to add two numbers I can write a comment “Addition of two numbers” which will help me in the near future when I need to reevaluate the code or any other programmer looks at my code. It will also help him understand what a certain function or a block of code does.

Watch our Demo Courses and Videos

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

Now there are three options in VBA to put comments in blocks of codes. We will learn each option by examples. But first, let me explain what these options are and how they work.

  1. The first option is the very simple one, we write any statement in a single quote (‘) it automatically becomes a comment in VBA.
  2. The second option is an inbuilt feature in VBA which needs to be activated which can comment or uncomment a block of words.
  3. The third option is also a very easy option, it is to use the REM keyword. REM keyword changes any statement into a comment in VBA.

Now I have explained the methods to block comments in VBA now let us use them in examples and try them.

But first, ensure that you have a developer’s tab enabled in order to use VBA. Developer’s tab can be enabled from the files tab in the options section.

Comment a Block of Code in Excel VBA

Below are the three processes to comment a block of code in Excel VBA and they are as follows:

  1. Using a single quote (‘)
  2. Using an inbuilt feature in the View tab
  3. Using Rem keyword
You can download this VBA Block Comment Excel Template here – VBA Block Comment Excel Template

VBA Block Comment – Example #1

Now let use the first method which is a very simple one using the single quote.

Step 1: As we discussed above in order to enter VBA we need to go to the Developer tab and click on Visual Basic to open VBA Editor.

VBA Block Comment Example 1-1

Step 2: Now once we are in VBA we need to insert a module. Module in VBA is where we write our blocks of codes.

VBA Block Comment Example 1-2

Step 3: Once we double click on the module we can see the code window. Start by declaring the name of the macro to start the code.

Code:

Sub Sample()

End Sub

VBA Block Comment Example 1-3

Step 4: Let us write a sample code then we will insert comments in it. Have a look at below the sample code I wrote.

Code:

Sub Sample()

Worksheets("Sheet1").Activate
Range("A1").Value = 20
Range("B1").Value = 30
Range("C1").Value = Range("A1").Value + Range("B1").Value

End Sub

VBA Block Comment Example 1-4

Step 5: Now after each line of code use single quote and write comments like the below screenshot.

Code:

Sub Sample()

Worksheets("Sheet1").Activate 'To activate worksheet
Range("A1").Value = 20
Range("B1").Value = 30 
Range("C1").Value = Range("A1").Value + Range("B1").Value

End Sub

VBA Block Comment Example 1-5

Step 6: We can see that the comment we wrote is in green color and we do not need to end it in a single quote. Now we can insert multiple comments as follows.

Code:

Sub Sample()

Worksheets("Sheet1").Activate 'To activate worksheet
Range("A1").Value = 20 'Value of A1
Range("B1").Value = 30 'Value of B1
Range("C1").Value = Range("A1").Value + Range("B1").Value 'Addition of A1 and B1

End Sub

VBA Block Comment Example 1-6

Step 7: We can run the above code and see that only the block of code gets executed not the comments. Press F5 to see the result.

Result of Example 1-7

This was the simplest way to insert comments in VBA.

VBA Block Comment – Example #2

Now we will use the second option which is to comment and uncomment from the inbuilt feature from View bar in VBA.

Step 1: As we discussed above in order to enter VBA we need to click on the Developer’s tab.

Step 2: Now we have already inserted our module so we do not need to insert a new module. We can work on the same module.

Step 3: Start by naming the macro so that we can write the code.

Code:

Sub Sample1()

End Sub

Example 2-1

Step 4: Now write a sample code as below and then we will comment on it.

Code:

Sub Sample1()

Dim A, B, C As Integer
A = 10
B = 20
C = A + B
MsgBox C

End Sub

VBA Block Comment Example 2-2

Step 5: Now write the comments on the codes as follows.

Code:

Sub Sample1()

Dim A, B, C As Integer 'Declaring three variables
A = 10
B = 20
C = A + B 'Adding them
MsgBox C

End Sub

VBA Block Comment Example 2-3

Step 6: Now go to the View tab and then to Toolbars as follows,

Toolbar Example 2-4

Step 7: In the toolbars section we can find the option for Edit as follows, Check the option for Edit.

Edit Menu Example 2-5

Step 8: When we click on it we get a rectangular wizard box as follows which as an option to make a line as a comment,

VBA Block Comment Example 2-6

Step 9: Select the code which we want to make it as a comment and then click on comment block as follows,

VBA Block Comment Example 2-7

Step 10: Now run the code by pressing the F5 Key and we can see that the code is not executed.

Result of Example 2-8

We will get a blank result as the line which had values and declared the variables is not termed as comments. In order to make the code to run we need to uncomment the statements.

VBA Block Comment – Example #3

Now we will use the third option which is the REM keyword.

Step 1: As we discussed above in order to enter VBA we need to click on the Developer’s tab.

Step 2: Now we have already inserted our module so we do not need to insert a new module. We can work on the same module.

Step 3: Start by naming the macro so that we can write the code.

Code:

Sub Sample2()

End Sub

VBA Block Comment Example 3-1

Step 4: Write a sample code as follows.

Code:

Sub Sample2()

Dim A, B, C As Integer
A = 10
B = 20
C = A + B
MsgBox C

End Sub

VBA Block Comment Example 3-2

Step 5: Now use REM keyword to insert comments as follows.

Code:

Sub Sample2()

Dim A, B, C As Integer
Rem declaring variables
A = 10
B = 20
C = A + B
Rem adding them
MsgBox C

End Sub

VBA Block Comment Example 3-3

Step 6: Now press F5 to run the code and see the result.

Result of Example 3-4

We can see that the comments were not executed but the codes were.

Things to Remember

  • Starting with single quote adds the comment and it doesn’t mean we have to end it with a single quote.
  • Comments are not executed in VBA.
  • Codes which are turned into comments are also not executed.

Recommended Articles

This is a guide to VBA Block Comments. Here we discuss three processes to comment a block of code in Excel VBA along with practical examples and downloadable excel template. You can also go through our other suggested articles –

  1. VBA Font Color
  2. VBA RGB
  3. VBA XML
  4. VBA While Loop

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 Block Comment Excel Template

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW