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.

Downloadable Files: Excel File

Question? Ask it in our Excel Forum

Our Excel Courses

  • Skill Level: All Levels
  • |
  • 5 hours+

How to make a fully featured professional form in Excel that is unbreakable. This includes how to use the form to store, view, edit, and delete data from a data storage worksheet.

  • Skill Level: All Levels
  • |
  • 2 hours

Send Emails from Excel using VBA and Macros. This course starts from the Basics and builds up to more advanced examples with attaching workbooks, worksheets, PDF's, automatically sending emails, including a signature, error handling, increasing speed, and more.

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 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...
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...
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...
Add Background Photos / Images to Comments in Excel
Macro: Add background images and photos to your comments in Excel with this macro. This macr...
Print Comments in Excel
Tutorial: How to print the comments in a worksheet when you print from Excel. This includes how to p...