Comments in Macros in Excel

Add to Favorites
Author:

How to put comments into a macro so that it's easier to understand and maintain them.

Comments allow you to make notes within your macro and VBA code. These notes do not affect the execution of the macro at all and, when the macro is run, Excel pretends that the comments aren't even there.

Sections:

How to Make Comments in Macros

When should you Use Comments in Macros?

Notes

How to Make Comments in Macros

To make a comment, just start any line with a single quotation mark or apostrophe (').

bd678df19638a7141758b4b07318f225.jpg

Here, I have three lines of comments. In the editor window, comments appear green.

You can have as many comments as you need inside a macro, the only rule is that each line must start with a comma/single quote.

Comment Part of a Line

You can have executable code and a comment on the same line. To do this, the comment must come AFTER the code.

6a0ec6a5d8e9f19d380380823f08377e.jpg

The code MsgBox "Hi!" will execute because it does not have a comma in front of it but the green text behind it is a comment because that has a comma in front of it.

Quickly Turn a lot of Code into a Comment

Sometimes when you are building a macro and testing it you might want to make some of the code a comment. You do this so that that code will not run; this allows you to test other code without having to delete the first set of code.

When you turn code into a comment, it's called "commenting-out" the code. Below, I will comment-out some code.

You can quickly turn multiple lines of code into comments like this:

  1. Select the code that you want to comment-out.
    8d38f9cea02a712855118b43b01c1350.jpg
  2. Click the Comment Block button on the edit toolbar. (If you don't see this toolbar, go to View > Toolbars > and click Edit.)
    2b51b7deba6cca980be34b461903242b.jpg
    Now, the code you selected will become a comment.
    5d5ed1554421beace8ad26ce32445c4c.jpg

Remove Comments from Multiple Lines at Once

Select the code to uncomment and go to the Edit toolbar and click the Uncomment Block button.

(If you don't see this toolbar, go to View > Toolbars > and click Edit.)

014d3624a803f0d46070d7b663fc897b.jpg

When should you Use Comments in Macros?

My general rule is "comment everything that is not intuitive or that you might forget."

Basically, you want to add comments around your code so that you better understand what is happening within your code.

At first, this may seem stupid because you know what your code should do because you just wrote it. BUT, think about when you might need to edit the macro in a year, will you still remember everything then?

When I program, I comment the hell out of everything and this has helped me tremendously. It allows me to look at an old project and immediately know what's going on, why I did this instead of that, what can or can't go somewhere, etc.

Adding comments to your code is one of the most important things you can do in order to make your code easy to maintain and update in the future.

Here is an example of commenting on the code in a macro:

b1f1fb8cb02dd63784979c7fcdd8bc5d.jpg

This is ridiculously simple code once you know even a little VBA, so, in reality, I would not add any comments to it. However, this gives you a sense of how you can add comments.

Comments should be interspersed within your code. Sometimes, if you are working on a complex project, there may be more comments than actual code, and that is OK. Make your comments so that you will understand what is going on, that is the only thing that matters when it comes to comments.

Notes

Comments are vital to making good macros. Without comments, you will be lost when you come back to old code that does anything that isn't blatantly obvious.

When in doubt, add a comment.

Download the Excel file for this tutorial to get the examples in Excel.


Downloadable Files: Excel File

Question? Ask it in our Excel Forum


Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons 50+ Hours of Instruction 200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

Similar Content on TeachExcel
Variables in Macros VBA for Excel - Practical Guide
Tutorial: This is a practical guide to using Variables in Macros and VBA for Excel. I will tell you ...
Create a Column Chart with a Macro in Excel
Macro: This macro adds a column chart to Excel. This is an easy to use macro that allows you to q...
Create a Bar Chart With a Macro in Excel
Macro: Create a bar chart in Excel with this macro. You will be able to quickly and easily turn a...
5 Simple Tips for Making BETTER Macros in Excel!
Tutorial: 5 ways to make better Macros in Excel - fewer errors, smoother coding, less fuss! These 5...
Add Background Photos / Images to Comments in Excel
Macro: Add background images and photos to your comments in Excel with this macro. This macr...
Delete Comments in Excel - Permanently Remove Specific Comments from Excel
Macro: Delete a comment in Excel with this macro. You can permanently remove or delete a specific...
Tutorial Details
Downloadable Files: Excel File
Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course