The error means VBA has hit a name it doesn't recognise as a variable, procedure, constant or object member. Nine times out of ten the fix is one of three things: you typed the name differently from how you declared it, the variable is declared inside a different procedure so it isn't visible here, or Option Explicit is on and you never declared it at all. Declare it with Dim at the top of the routine, make sure the spelling matches, and the error disappears.

This guide walks through finding the offending line, fixing the declaration, dealing with scope problems, and the handful of cases where the name isn't a variable at all — for example a missed reference or a function that used to spell-check differently in an older Excel.

What You'll Need

  • Excel with the workbook open and a password you know if the project is protected.
  • Access to the VBA editor — press Alt+F11 from Excel (on a Mac with a full keyboard, Option+F11; the menu path Development > Visual Basic is the same idea but menu names vary slightly by version and platform).
  • The error dialog still on screen, or the yellow-highlighted line if you've already dismissed it.
  • If the code lives in a password-protected VBA project, the project password — otherwise you can't edit the code and the fix has to come from whoever holds it.

Step-by-Step

  1. Read the highlighted line. When the error fires, VBA highlights the line it stopped on in yellow. The name that triggers "Variable not defined" is the one on that line that you can't see declared anywhere. If you already dismissed the dialog, use Debug > Compile VBAProject from the VBA editor to make VBA re-check the code and highlight the offending line again.
  2. Decide which category the name falls into. It will be one of: a variable you meant to create, a procedure you meant to call, an object property or method, a constant, or an array element. Each has a different fix, so this decision saves time.
  3. If it's a variable, declare it. At the top of the procedure, add Dim with a type, for example Dim totalRows As Long. Use Long for row numbers and counters — Integer overflows past about 32,767 rows and silently becomes a problem months later. Use String for text, Double for decimals, Boolean for True/False, and Range or Worksheet for objects (declared without New, then Set before use).
  4. Check the spelling character by character. VBA is case-insensitive for names but not typo-tolerant. totalRows and totlRows are different names. The editor's automatic capitalisation is your ally: when you type a name that matches an existing declaration, VBA rewrites it to the declared capitalisation as soon as you press Enter. If your name stays lowercase when a similar one elsewhere is capitalised, you have a typo — that mismatch is the fastest confirmation.
  5. Check where the declaration lives. A Dim inside Sub A is invisible to Sub B. If both routines need the value, either pass it as an argument or move the declaration to the top of the module as a module-level variable using Private (visible across the whole module but not outside it) or Public in a standard module (visible across the project). Declare module-level variables above the first Sub or Function in the module, not inside one.
  6. Handle Option Explicit deliberately. To declare every variable at the module level: Tools > Options in the VBA editor, then on the Editor tab tick Require Variable Declaration. That inserts Option Explicit at the top of every new module. It is worth keeping on — it turns typos into immediate errors instead of silently empty variables. If an existing codebase was never written with it and you can't declare everything right away, you can remove the single Option Explicit line at the top of that module, but treat that as a temporary fix and expect latent bugs.
  7. Fix object declarations properly. For a worksheet, Dim ws As Worksheet then Set ws = ThisWorkbook.Worksheets("Sheet1"). Forgetting Set produces a different error ("Object variable or With block variable not set"), but forgetting the Dim altogether produces exactly the error in this guide. Keep the two straight: not declared vs. declared but not assigned.
  8. Check references if the name looks like a property or method. If the failing name belongs to an object library — for example something from a third-party Excel add-in — the project may not reference the library anymore. In the VBA editor, Tools > References lists available libraries; a missing one shows as "MISSING:" in the list. Unticking the missing entry or re-installing the library resolves it. Do not tick references at random; each one changes what names are valid.
  9. Compile before you run. Debug > Compile VBAProject catches undeclared names across the whole module before execution. Fix everything it flags, then run again.
  10. Test with the smallest input first. Run the routine against a copy of the workbook and a couple of rows of data. Confirm the declared variables hold the values you expect using Debug.Print into the Immediate window (Ctrl+G, or View > Immediate Window) before running it across the full sheet.

Common Mistakes

  • Declaring inside the loop instead of the procedure. A Dim inside a For block is still procedure-level in VBA, so it isn't an error — but declaring the same name twice in one procedure is, and it produces "Duplicate declaration in current scope". Declare once, at the top.
  • Assuming Dim gives a value. A declared String starts empty, a Long starts at 0, and an object variable starts as Nothing. Assign before you use, especially before ws.Range(...).
  • Relying on Variant by accident. Writing Dim x with no type gives a Variant. It works, but it hides type mistakes and is slower in tight loops. Name the type.
  • Confusing a named range with a variable. MyTotal might be a workbook name rather than a VBA variable; referencing it as a bare identifier fails. Use Range("MyTotal").Value instead.
  • Editing the wrong module. The same variable name can exist in several modules with different types. If a fix doesn't take effect, check the module name at the top of the code window matches the routine you're calling.
  • Forgetting that declared capitalisation is binding by habit. Copying code from another project can bring names that were declared there but not here — the error only appears on the line that uses them, which may be far from where you pasted.

Tips

  • Put Option Explicit at the top of every module you write from now on. It costs one line and prevents the whole class of silent typo bugs.
  • Use a consistent naming convention — strName, lngRows, wsData, rngTarget — so an undeclared name stands out when you scan the code.
  • Keep declarations at the very top of each procedure so the scope is obvious at a glance.
  • Pass values between procedures as arguments rather than relying on module-level variables. Module-level state makes bugs harder to trace.
  • When you paste code from elsewhere, run Debug > Compile VBAProject before running it. Compilation surfaces every undeclared name at once.
  • Keep a working copy of the workbook before large edits. Alt+F11 edits are saved with the workbook and there's no version history inside the VBA editor.

Troubleshooting

The error keeps returning on the same line after you added Dim. Either the declaration sits in a different procedure, or you declared a different name. Compare the name on the highlighted line with the declaration character by character, including trailing spaces.

The name is declared and still fails. Confirm the module is a standard module, not a class module or worksheet module where scope rules differ. Then check whether a second declaration with the same name exists elsewhere in the same scope.

It fails only for some users. Missing references and add-in dependencies are the usual reason. Check Tools > References on the machine that fails, and confirm any required add-in is installed there. Late binding — creating objects with CreateObject instead of an early-bound reference — avoids the reference dependency entirely, at the cost of losing IntelliSense on that object.

Compile succeeds but the run fails. That moves the problem from declaration to assignment: use Debug.Print or step through with F8 to watch values change line by line.

The project is locked. If the VBA project is password-protected, you cannot view or edit the code. The fix has to come from the owner of that workbook. Do not attempt to strip the protection.

If that did not work, copy the smallest routine that fails into a fresh workbook and test there. If it runs cleanly, the problem is an interaction with another module, reference or workbook-level name rather than the routine itself.

Frequently Asked Questions

What does "Variable not defined" actually mean in VBA?

VBA has encountered a name that isn't a declared variable, a known procedure, a constant or a valid member of a referenced library. With Option Explicit on, it also fires for any variable that was never declared.

Why does the error appear only with Option Explicit?

Without it, VBA silently creates an empty Variant for unknown names instead of complaining, so typos turn into logic bugs. With it on, every undeclared name is an immediate error.

Does declaring a variable with Dim set its value?

No. It only reserves the name and type. Numeric variables start at 0, strings are empty, booleans are False and object variables are Nothing until you assign them.

Why can't one sub see a variable declared in another sub?

Procedure-level Dim is local by design. Pass the value as an argument, or declare it at module level with Private above the first procedure.

Should I use Private or Public for module variables?

Use Private when only that module needs the value; it keeps the namespace clean. Use Public in a standard module only when other modules genuinely need it.

Why does the error mention a name I never wrote?

Most likely a referenced library is missing, so a property or constant that used to exist isn't recognised. Check Tools > References for an entry marked missing.