Tutorial Details
Downloadable Files: Excel File
Introduction to Programming Macros in Excel
First Steps
Getting and Inputting Data
Adding Logic to Macros
UDF- User Defined Functions
Speeding Up Macros

Comments in Macros in Excel

Add to Favorites

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.


How to Make Comments in Macros

When should you Use Comments in Macros?


How to Make Comments in Macros

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


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.


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.
  2. Click the Comment Block button on the edit toolbar. (If you don't see this toolbar, go to View > Toolbars > and click Edit.)
    Now, the code you selected will become a comment.

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


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:


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.


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.

Question? Ask it in our Excel Forum

Downloadable Files: Excel File