• Skip to primary navigation
  • Skip to main content
  • Skip to footer
WallStreetMojo

Wallstreet Mojo

Wallstreet Mojo

MENUMENU
  • Resources
        • Excel

          • Excel Functions
          • Excel Tools
          • Excel Tips
        • Excel
        • Financial Functions Excel

          • NPV in Excel
          • IRR in excel
          • PV in Excel
        • Financial-Functions-Excel
        • Lookup Functions Excel

          • VLOOKUP
          • HLOOKUP
          • Index Function
        • Lookup-Functions-in-Excel
        • Excel Charts

          • Pareto Chart in Excel
          • Gannt Chart in Excel
          • Waterfall Chart in Excel
        • Excel-Charts
        • VBA

          • VBA Left Function
          • VBA Paste Special
          • VBA Worksheet Function
        • VBA
        • Others

          • Resources (A to Z)
          • Financial Modeling
          • Equity Research
          • Private Equity
          • Corporate Finance
          • Financial Statement Analysis
  • Free Courses
  • All Courses
        • Certification Courses

          Excel Course certificate
        • Excel VBA All in One Bundle

          Excel-VBA-Certification-Course
        • Excel Data Analysis Course

          Excel-Data-Analysis-Course
        • VBA Macros Course

          VBA-Training-Course
        • Others

          • Basic Excel Training
          • Advanced Excel Course
          • Tableau Certification Course
          • Excel for Finance Course

          • Excel for Marketers Course
          • Excel for HR Managers
          • Excel for Power Users
          • View All
  • Excel VBA All in One Bundle
  • Login

VBA Comment Block

Home » VBA » VBA Message Functions » VBA Comment Block

By Sharmila Reddy Leave a Comment

VBA Block Comment

Comment Block of Code in VBA

Comments in VBA programming are statements that are not executed or compiled but are only there to provide a brief description of any function, variable, statement, etc. These comments are not mandatory but are used so as to make the VBA code more informative, readable, organized and easy to understand. Also, if we do not wish to delete certain lines of code, and neither want them to be executed, then we can comment on them. In this article, we learn how to Comment block of code in Excel VBA.

Examples of Comment Block in Excel VBA

Let us see below how a single line or a block of lines can be commented in excel VBA.

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

Example #1 – Comment using Apostrophe

Let us say we wish to comment on a single line/statement in a VBA code. To do this, the Visual Basic Editor (VBE) has to be configured.

The Visual Basic Editor can be accessed as follows:

Go to Developer tab, and then click on Visual Basic Editor, or press Alt+F11 to open the Visual Basic Editor window.

VBA Block Comment Example 1

On doing this, a window opens as follows:

VBA Block Comment Example 1-1

Right click on the workbook name in the ‘Project-VBAProject’ pane and then click on ‘Insert’-> ‘Module’ as follows.

VBA Block Comment Example 1-2

Now we can write our code or procedure in this module:

Code:

Sub macro()

 'This is a Comment

End Sub

VBA Block Comment Example 1-3

So we can see in the above screenshot that on writing this code in the module, we see that when we simply put or insert an apostrophe before a statement/line, then that statement turns into green text and is considered as a  comment. So, we see that when we wish to comment on a single line, it can simply be preceded with an apostrophe.

This method can also be used to comment on multiple lines by putting an apostrophe before each line as follows:

Popular Course in this category
Cyber Monday Sale
VBA Training (3 Courses, 12+ Projects) 3 Courses | 12 Hands-on Projects | 43+ Hours | Full Lifetime Access | Certificate of Completion
4.6 (247 ratings)
Course Price

View Course

Related Courses

VBA Block Comment Example 1-4

Example #2 – Comment Using Toolbar

Now, let’s say we wish to skip over and comment an entire block of code, or multiple statements of the code. In such a case, using an apostrophe before each statement would be quite tedious and time-taking when we have so many statements to comment. So to do this, there is a built-in option of ‘Comment/Uncomment Block’ in VBE that is initially hidden in the toolbar and can be used as follows:

Select the statements in the macro/procedure that are required to be commented as below.

VBA Block Comment Example 2

Then click on ‘View -> ‘Toolbars’ -> ‘Customize’.

VBA Block Comment Example 2-1

This will generate or open a ‘Customize’ pop-up window. Now click on ‘Commands’ -> ‘Edit’, and then click on ‘Comment Block’ and drag it to the toolbar as follows.

VBA Block Comment Example 2-2

With this, we now have the ‘Comment Block’ icon on the Toolbar in VBA for its easy access.

Now click on the ‘Comment Block’ from the toolbar as follows:

VBA Block Comment Example 2-3

In doing so, the highlighted statements/lines would now be commented and turn out to be green in color as below:

Code:

Sub CommentLines()

 'MsgBox "First Comment Line"
 'MsgBox "Second Comment Line"
 'MsgBox "Third Comment Line"

End Sub

VBA Block Comment Example 2-4

So we can see in the above screenshot that the green statements will not be executed by the macro and will only be treated as comments.

Example #3 – Comment Using REM Keyword

Another method that can be used to make a statement/line as a comment is to add the keyword ‘REM’ before it.

Let us see below how this works:

VBA Block Comment Example 3

We can see in the below screenshot that when the keyword ‘REM’ is added before the statement: ‘This is a comment’, then it turns out to be green and hence a comment.

VBA BlockComment Example 3-1

Now let us see how this keyword can be used to comment multiple lines in the below screenshot.

Code:

Sub CommentUsingRem()

 Rem This is a Comment
 Rem This is a Comment
 Rem This is a Comment

End Sub

VBA BlockComment Example 3-2

So, we can see that apart from using apostrophe and ‘Comment Block’, the keyword ‘REM” can also be used to comment statements of code or procedure. However, using the keyword ‘REM’ has some limitations:

  • Space is mandatory between the keyword ‘REM’ and the start of the statement
  • It always has to be the first word to start with and cannot be used somewhere in the middle of a line/statement so as to comment rest of the line.

Example #4 – UnComment the Commented lines Using Toolbar

Just the way we can comment a block of lines at one go using the ‘Comment Block’ section in the toolbar, we can also uncomment the commented lines using the VBE built-in ‘Uncomment Block’ option in the same way as follows:

Select the commented statements in the macro/procedure that are required to be uncommented as below:

Block Comment Example 4

Now select ‘View -> ‘Toolbars’ -> ‘Customize’.

Example 4-1

This will generate or open a ‘Customize’ pop-up window. Now click on ‘Commands’ -> ‘Edit’, and then click on ‘Uncomment Block’ and drag it to the toolbar as follows:

Example 4-2

With this, we now have the ‘Uncomment Block’ icon on the Toolbar for its easy access.

Now click on the ‘Uncomment Block’ from the VBA toolbar as follows:

Example 4-3

In doing so, the highlighted statements that commented would now turn into executable statements of the code or procedure and change in color from green to black again as below:

Code:

Sub UncommentedLines()

 MsgBox "First Comment Line"
 MsgBox "Second Comment Line"
 MsgBox "Third Comment Line"

End Sub

VBA BlockComment Example 4-4

So these statements are no longer now comments.

Things to Remember

  • Comments are brief explanatory statements that can be used to describe the procedures.
  • Commenting blocks of VBA code can be useful in debugging the codes.
  • Any statement in the VBA code that follows an apostrophe is considered a comment.
  • As a good programming practice, comments can be used before each section of the code or before variable declarations and functions to describe their purpose.
  • The VBA Editor makes the statement’s font color as green to indicate that it is a comment.
  • Unless the apostrophe is present in a string, the compiler ignores the statement following an apostrophe until the end of the line.
  • An apostrophe can even be present somewhere in the middle of a line. Text after the apostrophe will be treated as a comment in that case.

Following screenshot illustrates this:

Apostrophe Comment

  • The comments do not affect code performance.
  • The comment symbol: Apostrophe’, or “REM” has to be used on each line if the comments require more than one line.
  • By default, the comments appear as green color in the Code window.
  • The advantage of using apostrophe and ‘Comment Block’ over the keyword ‘REM’ is that they need less memory and space and are also more easier to use.

Recommended Articles

This has been a guide to VBA Block Comment. Here we learn 3 ways to comment/uncomment blocks of VBA codes 1)Using Apostrophe, 2)Using Toolbar, 3) Using REM Keyword along with practical examples and downloadable excel template. Below you can find some useful excel VBA articles –

  • VBA Workbook Open
  • Excel Examples of VBA INSTRREV Function
  • VBA Chr Function Syntax
  • VBA RegEx
  • Insert Comment Examples in Excel 
  • Print Comments Examples in Excel
  • VBA Workbook Object
  • Find Used Rows & Columns in ExcelVBA
3 Shares
Share
Tweet
Share
VBA Training (3 Courses, 12+ Projects)
  • 3 Courses
  • 12 Hands-on Projects
  • 43+ Hours
  • Full Lifetime Access
  • Certificate of Completion
LEARN MORE >>

Filed Under: VBA, VBA Message Functions

Reader Interactions
Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Footer
COMPANY
About
Reviews
Blog
Contact
Privacy
Terms of Service
FREE COURSES
Free Finance Online Course
Free Accounting Online Course
Free Online Excel Course
Free VBA Course
Free Investment Banking Course
Free Financial Modeling Course
Free Ratio Analysis Course

CERTIFICATION COURSES
All Courses
Financial Analyst All in One Course
Investment Banking Course
Financial Modeling Course
Private Equity Course
Business Valuation Course
Equity Research Course
CFA Level 1 Course
CFA Level 2 Course
Venture Capital Course
Microsoft Excel Course
VBA Macros Course
Accounting Course
Advanced Excel Course
Fixed Income Course
RESOURCES
Investment Banking
Financial Modeling
Equity Research
Private Equity
Excel
Books
Certifications
Accounting
Asset Management
Risk Management

Copyright © 2019. CFA Institute Does Not Endorse, Promote, Or Warrant The Accuracy Or Quality Of WallStreetMojo. CFA® And Chartered Financial Analyst® Are Registered Trademarks Owned By CFA Institute.
Return to top

Free Excel Course

Excel functions, Formula, Charts, Formatting creating excel dashboard & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.

Free Excel Course

Excel functions, Formula, Charts, Formatting creating excel dashboard & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.

Free Investment Banking Course

IB Excel Templates, Accounting, Valuation, Financial Modeling, Video Tutorials

By continuing above step, you agree to our Terms of Use and Privacy Policy.

Free Investment Banking Course

IB Excel Templates, Accounting, Valuation, Financial Modeling, Video Tutorials

By continuing above step, you agree to our Terms of Use and Privacy Policy.
WallStreetMojo

Free Investment Banking Course

IB Excel Templates, Accounting, Valuation, Financial Modeling, Video Tutorials

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

WallStreetMojo

Free Investment Banking Course

IB Excel Templates, Accounting, Valuation, Financial Modeling, Video Tutorials

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

WallStreetMojo

Free Investment Banking Course

IB Excel Templates, Accounting, Valuation, Financial Modeling, Video Tutorials

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

WallStreetMojo

Free Excel Course

Excel functions, Formula, Charts, Formatting creating excel dashboard & others

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

WallStreetMojo

Download VBA Block Comment Template

By continuing above step, you agree to our Terms of Use and Privacy Policy.

Limited Period Offer - VBA Training Course (6 courses, 35+ hours video) View More