Excel VBA Must Be First Statement on Line?

VBA & macros · Updated November 22, 2026

No, the first statement on a line does not have to be a VBA statement at all — but if you put a statement after a line label or a colon, it must be separated correctly. In VBA, a line can contain a label, a comment, or a statement, and you can combine multiple statements on one line using colons. The only rule is that a line label must be the first thing on its line (unless it is preceded by a colon), and a comment can appear anywhere after an apostrophe. This article explains the exact rules, common pitfalls, and how to format your code correctly.

What the VBA Line Structure Rules Actually Are

VBA treats each physical line as a single logical line unless you use line continuation. The grammar is simple:

  • A line can contain zero or more statements, separated by colons (:).
  • A line can start with a line label (an identifier ending with a colon, e.g., Start:), and that label must be the first token on the line — you cannot put a statement before a label on the same line.
  • A comment (starting with an apostrophe ') can appear anywhere after a statement or label, and everything after the apostrophe is ignored.
  • You can put a statement after a label on the same line only if you use a colon after the label, e.g., Start: Debug.Print "hi" — but that is a label followed by a statement, not a statement before a label.

So the answer is: no, the first statement does not have to be a VBA statement — it can be a label or a comment. But if you have a label, it must be the first thing on the line (or after a colon if you are using a line continuation).

Why This Confusion Exists

Many beginners see error messages like “Expected: expression” or “Expected: statement” when they try to place a label after a statement on the same line. For example, this fails:

Debug.Print "Hello"
GoTo Skip
Skip: Debug.Print "World"  ' This is fine

But this also fails:

Debug.Print "Hello" : Skip: Debug.Print "World"  ' Error: label not allowed after a statement on the same line

The VBA compiler requires that a line label be the first token on a line, unless it is preceded by a colon that itself is the first token (which is unusual). The reason is that a label is a marker for a jump target, and the parser needs to know where it starts. If you put a statement before it, the parser cannot distinguish the label from an identifier in an expression.

How to Correctly Combine Statements and Labels

If you need to jump to a label, always put the label on its own line, or at the start of a line after a colon if you are combining with a statement. Here are the valid patterns:

  • Label on its own line:
    Skip:
    Debug.Print "World"
  • Label followed by a statement on the same line using a colon:
    Skip: Debug.Print "World"
  • Multiple statements on one line using colons:
    Debug.Print "A" : Debug.Print "B"
  • Comment at the end of a line:
    Debug.Print "Hello" ' This is a comment

Note that you cannot put a statement before a label on the same line, even with a colon. That is the only hard restriction.

Common Misconceptions About Line Continuation

Another related rule is line continuation. If you want to split a long statement across multiple lines, you use an underscore _ at the end of the line (with a space before it). But you cannot put a comment after the underscore, and you cannot continue a line that is inside a string literal. For example:

Dim total As Long
 total = 1 + 2 + _
        3 + 4  ' This works

But this fails:

total = 1 + 2 + _ ' comment here is not allowed
        3 + 4

Also, you cannot use line continuation inside a string. Instead, use string concatenation:

MsgBox "Hello " & _
       "World"  ' Works

But MsgBox "Hello _
World"
will not work because the underscore is inside the string literal.

Practical Examples: When You Might Need a Label

Labels are typically used with GoTo for error handling or looping. A common pattern is:

Sub Test()
    On Error GoTo ErrorHandler
    Dim x As Integer
    x = 1 / 0  ' Causes error
    Exit Sub
ErrorHandler:
    MsgBox "An error occurred"
End Sub

Here, the label ErrorHandler: is on its own line. If you tried to put it on the same line as the Exit Sub statement, it would fail. Always keep labels on separate lines for clarity and to avoid syntax errors.

How to Avoid Syntax Errors with Colons and Labels

If you are combining multiple statements on one line, be careful not to accidentally create a label. For instance, if you write:

Dim i As Integer : i = 5 : If i > 3 Then GoTo Skip : Skip: MsgBox "Done"

This will fail because after the GoTo Skip statement, the label Skip: is not the first token on the line. Instead, write it as:

Dim i As Integer : i = 5 : If i > 3 Then GoTo Skip
Skip: MsgBox "Done"

Or, if you really want one line, you can use a colon before the label, but that is unusual and not recommended:

Dim i As Integer : i = 5 : If i > 3 Then GoTo Skip : Skip: MsgBox "Done"  ' Still fails

Actually, the only way to have a label after a statement on the same line is to put a colon immediately before the label, but then the label is still not the first token — it is after a colon. The VBA parser does allow a label after a colon, but only if the colon is the first character on the line? Let’s test: :Skip: Debug.Print is valid? In practice, VBA accepts a label after a colon if the colon is at the start of the line, but it is not a good practice. The safest is to put labels on their own lines.

Frequently Asked Questions

Can I put a comment before a statement on the same line? Yes, you can put a comment at the beginning of a line, but then the line has no executable statement. For example, ' comment is a line with only a comment. If you want a comment and then a statement on the same line, you must put the statement first, then the comment: Debug.Print "Hi" ' comment. You cannot put a comment before a statement on the same line because everything after the apostrophe is ignored, so the statement would be commented out.

What is the difference between a line label and a variable name? A line label ends with a colon, and it is used only as a target for GoTo or Resume. A variable name does not end with a colon and holds a value. You cannot use the same name for both a label and a variable in the same scope.

Is it bad practice to put multiple statements on one line? It is generally discouraged because it hurts readability and debugging. However, it is legal. If you do it, use colons sparingly and always add comments to clarify.

Can I use a line label inside a For loop? Yes, you can, but you must be careful with scope and flow. For example, you can use GoTo to jump out of a loop, but jumping into a loop is not allowed and will cause a compile error.

Does the rule change in Excel VBA vs. other Office applications? No, the VBA language is the same across all Office applications. The line structure rules are identical.

What happens if I forget to put a colon after a label? If you write Skip without a colon, VBA treats it as a variable name, not a label. Then GoTo Skip will cause a compile error because Skip is not a label.

Final Tips for Writing Clean VBA

To avoid confusion, follow these simple guidelines:

  • Put each label on its own line, with the colon immediately after the name.
  • Use comments on separate lines or after statements, not before.
  • Use line continuation only when a statement is too long, and never put a comment after the underscore.
  • When combining statements with colons, keep them short and logical.
  • Always save a copy of your workbook before running a macro, because macros cannot be undone with Ctrl+Z.

Remember that VBA is forgiving, but it has strict parsing rules. Understanding these rules will save you from cryptic error messages.

Related

Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.

More in VBA & macros

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.