🚀The world's best VBA AI has evolved. ExcelMaster is now an autonomous Agent.Read more →
Back to Blog

VBA Comment in Excel — Comment Out Code, the Apostrophe Rule & Why It Never Runs

|

VBA Comment in Excel — Comment Out Code, the Apostrophe Rule & Why It Never Runs

TL;DR — Put an apostrophe ' in front of any line and everything after it becomes a comment: the compiler deletes it before your macro runs. Two jobs — leave a note, or temporarily disable code without erasing it. There is no /* */ block syntax; to switch off a whole block you prefix every line with ' (the Comment Block button on the Edit toolbar does it in bulk). Comment why, never what — a comment that restates the code is a lie waiting for the code to change.

Sub CommentBasics()
    ' This whole line is a note - the compiler ignores it
    Dim total As Long
    total = 100            ' a trailing comment explains the WHY, not the what

    ' MsgBox total         <- disabled, not deleted: remove the ' to switch it back on
End Sub

What you'll learn

  • The mental model — a comment is the one line guaranteed never to run
  • The three source-only tokens, and why beginners underrate all of them
  • Apostrophe ' vs the older Rem, and when each is legal
  • How to comment out a whole block (and where the button is hiding)
  • Why a comment breaks a line-continuation — and why that trap is silent
  • A source comment vs the cell Comment / Note object — two different things
  • The opinion: the only comments worth the pixels

The mental model: the one line that never runs

Every other line you write is an instruction to the macro. A comment is not. The moment the VBA compiler sees an apostrophe, it discards the rest of that line — it is never parsed, never compiled, never executed. That single guarantee is the whole point, and it powers the two jobs a comment does:

  1. Explain intent to the next human (often future you), and
  2. Disable code without deleting it, so you can test a change and put the old line back in one keystroke.

Because a comment is invisible to the compiler, commenting out a line is the safest edit in VBA: nothing is lost, nothing is risked. And because it is invisible to the compiler, a comment that is wrong will never be caught — no error, no warning, ever. Hold both of those in your head and the rest of this guide follows.

Three tokens the macro never runs

A comment is not a lonely feature — it is one of three things you type that the running macro never executes. They shape the source and the compile, not the run, which is exactly why newcomers dismiss them as trivia. They are the difference between VBA you can maintain and VBA that rots.

Token What it does to your source What it costs the run
' comment Leaves notes and disables code the compiler deletes Nothing — it never runs
_ line-continuation Splits one long statement across many lines Nothing — the parser erases it before compiling
Option Explicit Forces every name to be declared Runs at compile time, so typos die before the macro starts

Master the three and typos die before they run, a 300-character line stops scrolling off-screen, and dead code toggles with a single apostrophe. This guide is the first rung; the other two are one click away.

Apostrophe vs Rem: use the apostrophe

VBA has two comment markers, and they are not equal.

' The apostrophe works anywhere - whole line or end of a line
x = 5          ' explains this assignment

Rem The Rem statement works only as a WHOLE statement
x = 5 : Rem you need a colon to put Rem after code
Rem x = 5      ' <- Rem cannot sit at the end of a line without a colon

The apostrophe ' is the modern, universal marker: it is legal at the start of a line or hanging off the end of any statement. Rem is a leftover from early BASIC — it is a statement, so it must begin a statement, and to place it after code you need a colon separator (x = 5 : Rem note). There is no reason to reach for Rem in new code. Learn the apostrophe and forget Rem exists, except for the one time you inherit it in someone else's macro.

Comment out a block: the button is hidden

VBA has no /* ... */ block-comment syntax. To disable several lines you put an apostrophe in front of each one:

'    total = 0
'    For i = 1 To n
'        total = total + data(i)
'    Next i

Doing that by hand is tedious, which is why beginners delete code to test whether it is the problem — and then cannot get it back. Don't. The VBA editor ships with Comment Block and Uncomment Block buttons that apostrophe or un-apostrophe every selected line at once. The catch: they are on the Edit toolbar, which is not shown by default. Turn it on once with View > Toolbars > Edit, and disabling a ten-line block becomes select-and-click. This is the single most useful debugging habit in VBA: never delete code to test a theory — comment it out, run, then uncomment.

The silent trap: a comment kills a line-continuation

Here is the one that costs an afternoon. You cannot put a comment in the middle of a statement that is split across lines with the _ continuation, because the apostrophe ends the logical line — and it swallows the _ along with it.

' BROKEN - the comment eats the continuation
total = price _   ' add the base price
      + tax        ' <- "+ tax" is now a separate, broken statement

' RIGHT - keep comments off continued lines, or put one above
' add the base price and tax
total = price _
      + tax

Nothing about this looks wrong at a glance, and the error message — Expected: end of statement on the + tax line, or a silently changed result — points at the wrong place. The rule: a _ line-continuation and a comment cannot share a physical line. Put the explanation on its own comment line above the statement. (This is the seam where two of our three source tokens collide; the line-continuation guide covers the other half.)

Source comment vs the cell Comment object

Two completely different things share the word comment, and searchers land here meaning either one.

  • A source comment (this article) — an apostrophe in your VBA code. It lives in the editor, documents or disables code, and never appears on the worksheet.
  • A cell Comment / Note — a yellow note attached to a cell in the grid, which you add in code with Range("A1").AddComment "text" (older) or through Range("A1").NoteText / the modern threaded Comments API. It is data on the sheet, visible to anyone who opens the file.

If you came here to annotate your code, stay — everything above applies. If you meant to drop a note on a cell from a macro, that is the AddComment / NoteText object model, a different topic entirely. Confusing the two is common precisely because Excel reused the word.

The opinion: comment why, never what

Most comments are noise, and the worst are actively harmful. i = i + 1 ' add one to i tells you nothing the code did not already say, and the day someone changes the line to i = i + step, the comment becomes a lie that no compiler will ever flag. A comment that restates the code rots faster than the code itself.

The comments that earn their pixels are the ones the code cannot say:

  • Why, not what — ' Suppliers send dates as text; parse before comparing explains a decision.
  • Units and assumptions' amount is in cents, not dollars prevents a real bug.
  • Edge cases and gotchas' returns Empty on a blank sheet - callers must handle it.
  • Disabled code with a reason and a date' 2026-09: dropped the retry, the API is stable now.

And often the best comment is no comment: a variable named totalCents beats total ' in cents, and turning on Option Explicit with real declarations documents intent better than a paragraph of prose. Write the code so it needs fewer comments, then make the few you keep about why.

When the real job is understanding the macro, not annotating it

Comments help you read code you already have. But half the time the actual task is the opposite — you are staring at a 300-line inherited macro with no comments, trying to work out what it does before you dare change it. Reading it line by line, sprinkling Debug.Print, and reconstructing the logic by hand can cost more than the change itself. ExcelMaster lets you skip that: describe what you want in plain English — "clean this column, remove the duplicates, and total the rest by month" — and it writes and runs the code, backing up your file first. You get the result without decoding someone else's uncommented VBA, and the code it generates is small enough to actually read.

Frequently asked questions

How do you comment out a line in VBA?

Put an apostrophe ' at the start of the line (or anywhere on it — everything after the apostrophe is ignored). To comment out several lines at once, select them and click Comment Block on the Edit toolbar (View > Toolbars > Edit to show it), which adds an apostrophe to every selected line.

What is the difference between an apostrophe and Rem in VBA?

Both mark a comment, but the apostrophe ' works anywhere — at the start of a line or hanging off the end of a statement — while Rem is a statement and must begin one, needing a colon to follow code (x = 5 : Rem note). Use the apostrophe; Rem only survives in legacy code.

How do I comment out a whole block in VBA?

There is no /* */ block syntax. Select the lines and use the Comment Block button on the Edit toolbar to prefix each with an apostrophe, and Uncomment Block to remove them. Show the toolbar with View > Toolbars > Edit if you do not see the buttons.

Why does my code break when I add a comment?

Almost always because you put the comment on a line that ends with a _ line-continuation. The apostrophe ends the logical statement and swallows the _, so the next line becomes a separate, broken statement. Move the comment to its own line above the continued statement.

Is a VBA comment the same as a cell comment?

No. A VBA (source) comment is an apostrophe in your code and lives only in the editor. A cell Comment or Note is a yellow annotation on a worksheet cell, added in code with Range.AddComment or Range.NoteText and visible in the grid. They share a name but are unrelated.

Tested in

Tested in: Excel 365 (Windows 11), VBA 7.1 — last verified 2026-09-06.

Related guides: VBA Option Explicit · VBA Line Continuation · VBA Dim · VBA Debug.Print · VBA MsgBox