Excel VBA Expected Error: How to Find and Fix It

VBA & macros · Updated November 20, 2026

Excel VBA has no single error called “expected error”; the message usually means the compiler expected a particular word, symbol, or expression and found something else. Read the full compile message, use the highlighted line as a starting point, correct the syntax or missing reference, and then compile the project again.

What an “Expected” VBA error means

VBA checks the structure of your code before it can run. A compile message such as “Expected: end of statement” or “Expected: expression” means that VBA cannot parse part of the code as written. The expected item in the message is a clue: it tells you what kind of token the compiler was looking for, not necessarily the exact character you need to add.

For example, a missing operator between two values, a misspelled keyword, an unmatched parenthesis, or an incorrectly placed line break can make VBA lose track of the statement. Sometimes the highlighted line is where VBA finally recognizes that the code is invalid, while the actual omission is on the previous line. Read a few lines above the highlight before editing.

This is different from a runtime error. A compile error prevents the procedure or project from running until the code is corrected. A runtime error occurs after execution has started—for example, when code tries to use a worksheet that does not exist. The fixes are different, so identifying which kind of error you have is the first step.

Find the exact message and highlighted code

When the error dialog appears, note its complete wording rather than relying on the phrase “expected error.” Choose Debug if the dialog offers it; the Visual Basic Editor (VBE) will usually take you to the line VBA could not compile. If you closed the dialog, open the editor from Excel with Alt+F11 on Windows, select the relevant module in Project Explorer, and inspect the code around the reported location.

To check the project explicitly, use Debug > Compile VBAProject in the VBE. If the project has a different name, the menu item uses that project name. Compilation may stop at the first problem, so fix it and run the compile command again. A second compile can reveal another issue that the first error had concealed.

  1. Record the full compile message, including any text after “Expected:”.
  2. Look at the highlighted line and the statements immediately before and after it.
  3. Check parentheses, commas, quotation marks, operators, colons, and line-continuation characters.
  4. Correct one issue, then compile again to confirm whether it was resolved.

On Excel for Mac, open the Visual Basic Editor from the Developer tab’s Visual Basic button where available; the keyboard shortcut can vary with the Mac keyboard and function-key settings. The VBE’s compile command and the underlying VBA syntax checks serve the same purpose. Excel for the web cannot create, edit, or run VBA macros, so open the workbook in desktop Excel to work on its VBA project.

Check common syntax causes

VBA statements have a defined structure. A line that looks understandable to a person may still be missing a separator or contain a token in the wrong position. These are common places to look:

  • Missing operators: If an expression combines values, confirm that the required operator, such as +, &, or =, is present between them.
  • Unbalanced parentheses: Count opening and closing parentheses in function calls and conditions. Nested expressions can make a missing closing parenthesis easy to overlook.
  • Incorrect commas or colons: Commas separate arguments in many calls, while a colon can separate statements on one line. A separator in the wrong place can change how VBA parses the statement.
  • Unclosed text: Each string literal must have matching straight double quotation marks. A missing closing quote can make later code appear to be part of the string.
  • Line continuation: To continue a VBA statement on the next line, use a space followed by an underscore at the end of the continued line. Do not put a comment after that underscore on the same line.
  • Misspelled or misplaced keywords: Check block pairs such as If and End If, and For and Next. Make sure declarations and procedure statements are in valid locations.

For instance, when you see “Expected: expression,” inspect the right-hand side of an assignment, a condition, or a function argument. When the message says “Expected: end of statement,” look for extra text after a statement, a missing operator, or punctuation that makes VBA think the line should already be finished. These are diagnostic directions, not guarantees: the exact cause depends on the surrounding code.

Check declarations, names, and references

Some compiler messages sound like syntax problems but point to names or declarations. If a variable is misspelled, undeclared, or used where a procedure name is expected, VBA may highlight the use rather than the place where the name was first introduced. Add Option Explicit at the top of each module to require variable declarations. It helps catch typing mistakes, although it does not repair incorrect punctuation or expressions.

Also check whether a name conflicts with a VBA keyword, a module name, or a procedure name. A variable called Date, for example, can make code harder to understand because Date is also a VBA function. Use clear names such as reportDate instead. If the message mentions a missing project, library, or user-defined type, open Tools > References in the VBE and look for an entry marked “MISSING.” Only select a replacement reference if you know which library the code is meant to use; removing or substituting references at random can create further problems.

For procedures that refer to workbook objects, qualify them where appropriate. An unqualified Range("A1") refers to the active sheet, which may be a logic problem rather than a compile error. A compile fix should address the reported parsing or declaration issue; improving object qualification can prevent a separate worksheet-selection bug.

Use a small, valid procedure to isolate the issue

If a long procedure is difficult to inspect, temporarily reduce it to the smallest section that still produces the compile message. Keep a copy of the original, then check whether declarations, procedure boundaries, and the key expression are valid. This simple example is a complete procedure; it assumes the intended values are in cells A1 and A2 on the active worksheet:

Option Explicit

Sub ShowSum()
    Dim total As Double

    ' Add the values from the two cells.
    total = Range("A1").Value + Range("A2").Value
    MsgBox total
End Sub

The example demonstrates a declared variable, an assignment, and a complete procedure. It is syntactically valid, but the unqualified ranges depend on whichever worksheet is active. For more predictable workbook code, explicitly set a worksheet variable and use it for both ranges. The worksheet name must match an existing sheet tab:

Option Explicit

Sub ShowDataSheetSum()
    Dim ws As Worksheet
    Dim total As Double

    ' Use the worksheet named Data in this workbook.
    Set ws = ThisWorkbook.Worksheets("Data")
    total = ws.Range("A1").Value + ws.Range("A2").Value
    MsgBox total
End Sub

This version compiles if the references and names are valid, but it can still produce a runtime error if the workbook has no sheet named Data. That distinction matters: the compile check confirms that VBA can interpret the code, not that every workbook object or input will be available when the macro runs.

Tell compile errors apart from runtime errors

A syntax or compile error must be corrected in the code; adding an error handler will not make an invalid statement compile. In particular, On Error Resume Next is not a fix for an “Expected:” message. That statement changes how certain runtime errors are handled after execution begins, and using it broadly can hide failures that should be investigated.

For a genuine runtime error, use a targeted handler when there is a sensible recovery action. A handler can capture the error, report it, and leave the procedure cleanly. This example shows the basic structure; its error handler reports an error rather than pretending the operation succeeded:

Option Explicit

Sub ExampleWithErrorHandler()
    On Error GoTo ErrorHandler

    ' Put an operation that can fail here.
    MsgBox ThisWorkbook.Worksheets("Data").Range("A1").Value
    Exit Sub

ErrorHandler:
    MsgBox "Error " & Err.Number & ": " & Err.Description
End Sub

The example can still raise an error if the Data sheet is absent, but the handler catches that runtime failure. It does not catch a compile error in the procedure. Use a handler for expected runtime conditions only when your code can respond appropriately, and avoid swallowing an error without recording or resolving it.

Common troubleshooting mistakes

Repeatedly changing unrelated lines is a poor way to diagnose a compiler message. Make one focused change at a time and compile again. If the message moves, the parser may have passed the original problem and found the next one; if it remains on the same line, inspect the syntax and names on that line as well as the preceding statement.

  • Fixing only the highlighted character: The cause can be an unfinished expression or quote on the line above.
  • Adding error handling to silence a compile message: Error handlers address runtime errors, not invalid VBA syntax.
  • Deleting a reference without checking its purpose: Other code may depend on that library or type.
  • Assuming the macro is broken because Excel will not run it: A disabled macro, an unsaved workbook format, or security settings can stop execution independently of compilation.
  • Editing only one copy of the code: Similar procedures may exist in another module, workbook, or add-in. Confirm which project and procedure are actually involved.

When a procedure appears valid but the project still will not compile, use Debug > Compile VBAProject and follow the next reported issue. Also review the VBE’s Project Explorer to confirm that you are editing the intended workbook. A workbook saved as .xlsx cannot store VBA macros; keep macro-enabled code in a format such as .xlsm when the workbook needs to retain its VBA project.

Prevent the same error from returning

Save a backup before making substantial changes, especially when the workbook contains important macros or is used by other people. Before running any macro that changes workbook data, save a copy of the workbook: macros cannot be undone with Ctrl+Z. On Mac, use Command+Z for ordinary undo, but it likewise does not reverse a macro’s changes.

After editing, compile the project before testing the macro on real data. Keep procedures small enough to inspect, use descriptive variable and procedure names, and declare variables with Option Explicit. If the code is shared across machines, check that required references are available on each system; a library present on one computer may be missing on another. Test on a copy with representative workbook content so that a runtime issue does not damage the working file.

Frequently Asked Questions

What does “Compile error: Expected” mean in Excel VBA?
It means VBA could not parse part of the code and expected a particular kind of syntax, such as an expression or the end of a statement. The wording after “Expected:” and the surrounding lines help narrow down the cause.

Why does VBA highlight the wrong line?
The highlighted line is where VBA detected that the code no longer fits its syntax rules. A missing quote, parenthesis, operator, or line continuation on an earlier line can cause the error to surface later.

Will On Error Resume Next fix an expected compile error?
No. It affects some runtime errors after a procedure begins running. Correct the syntax or declaration problem and compile the project again.

How do I compile VBA without running a macro?
Open the Visual Basic Editor and choose Debug > Compile VBAProject. Compilation checks the project without executing the macro’s normal operations, though it stops at the first compile problem it encounters.

Can I fix VBA errors in Excel for the web?
No. Excel for the web can preserve a workbook containing macros, but VBA editing and execution require desktop Excel. Open the workbook in Excel for Windows or Mac to inspect and compile its VBA project.

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.