Comments in Excel VBA
Learn how to add comments to your VBA code, explain your program logic and make your code easier to understand and maintain.
📘 What are Comments in VBA?
Comments are notes written inside your VBA code to explain what the code is doing. Comments are ignored by VBA when the program runs.
They are mainly used to make code easier to read, understand, debug and maintain.
🎯 Why Should You Use Comments?
As your VBA projects become larger, it can become difficult to remember why a particular piece of code was written. Comments help explain the purpose of your code.
- Explain what a VBA procedure does.
- Explain complicated programming logic.
- Make your code easier for other developers to understand.
- Help you remember your own code later.
- Temporarily disable a line of VBA code.
- Make debugging and maintenance easier.
✏️ Single-Line Comments
The simplest way to create a comment in VBA is by using an apostrophe:
Anything written after the apostrophe on that line is treated as a comment and will not be executed.
➡️ Comment After VBA Code
You can also place a comment after a line of VBA code.
📝 Using REM for Comments
VBA also supports the REM keyword for creating comments.
However, the apostrophe ' is generally preferred because it is shorter and easier to use.
🚫 Using Comments to Temporarily Disable Code
Comments can also be useful when testing or debugging your VBA program. You can temporarily disable a line of code by adding an apostrophe at the beginning.
In this example, the second message will not appear because the line has been converted into a comment.
✅ Good Comments vs ❌ Bad Comments
This comment simply repeats what the code already tells us.
This comment explains why the value is being increased.
💼 Real-World Example
Consider a VBA program that processes employee data. Comments can make the purpose of each section much clearer.
⭐ Best Practices for VBA Comments
- Write comments that explain why, not only what.
- Keep comments short and meaningful.
- Use comments to explain complex business logic.
- Update comments when you change your code.
- Do not add unnecessary comments to every simple line.
- Use comments to separate major sections of a large procedure.
📌 Quick Summary
- ' is the most common way to create a VBA comment.
- REM can also be used for comments.
- Comments are ignored when VBA executes the program.
- Comments make code easier to understand.
- Comments can temporarily disable code.
- Good comments explain the purpose or reason behind the code.
When you write professional VBA applications, don't only focus on making the code work. Write code that another person can understand and maintain.
🚀 Ready for the Next VBA Topic?
Practice comments by creating your own VBA macro and adding meaningful explanations to your code.
🏠 Back to VBA Roadmap
0 Comments