Comments

💬

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.

🟢 BEGINNER • VBA FUNDAMENTALS

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

Important: A comment does not execute as VBA code. It is only there for the programmer or anyone who reads the code later.

🎯 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:

' This is a VBA comment

Anything written after the apostrophe on that line is treated as a comment and will not be executed.

Sub MyFirstMacro() ' Display a message to the user MsgBox "Hello from VBA!" End Sub

➡️ Comment After VBA Code

You can also place a comment after a line of VBA code.

Sub CalculateTotal() Dim Total As Double Total = 100 + 200 ' Calculate the total MsgBox Total ' Display the result End Sub
Tip: Use comments after code when you want to briefly explain the purpose of a particular line.

📝 Using REM for Comments

VBA also supports the REM keyword for creating comments.

REM This is a comment Sub TestMacro() REM Display message MsgBox "Hello VBA" End Sub

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.

Sub TestMacro() MsgBox "First Message" 'MsgBox "Second Message" End Sub

In this example, the second message will not appear because the line has been converted into a comment.

✅ Good Comments vs ❌ Bad Comments

❌ Not Very Useful
i = i + 1 ' Add 1 to i

This comment simply repeats what the code already tells us.

✅ More Useful
i = i + 1 ' Move to the next employee record

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.

Sub ProcessEmployees() ' Find the last employee row Dim LastRow As Long LastRow = Cells(Rows.Count, 1).End(xlUp).Row ' Process employee records Dim i As Long For i = 2 To LastRow ' Calculate employee bonus Cells(i, 5).Value = Cells(i, 4).Value * 10% Next i ' Show completion message MsgBox "Employee processing completed." End Sub

⭐ 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.
'======================================== ' Employee Processing '======================================== ' Find last row LastRow = Cells(Rows.Count, 1).End(xlUp).Row

📌 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.
💡 VBA Tip:

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

Post a Comment

0 Comments