To debug an Excel VBA function, put the function in a standard module, open the VBA Editor with Alt+F11, set a breakpoint on the first executable line by clicking the grey margin or pressing F9, then run a small test macro that calls the function. When execution pauses, use Step Into (F8) to walk through each line and the Immediate Window (Ctrl+G) to print variables with ?variableName. If the function is called from a worksheet cell, the breakpoint is ignored until you call it from a macro instead, which is the most common reason debugging appears to do nothing.

A Function differs from a Sub in one key way: it returns a value and is usually called from a cell or another procedure, so you cannot simply press F5 inside it and watch it run alone. You need a wrapper Sub to feed it arguments, or the Debug toolbar's Evaluate tools. The steps below work for the desktop version of Excel for Windows and Mac; labels can differ slightly by version and region, and the VBA Editor is not available in Excel for the web or mobile.

What You'll Need

  • Excel for Windows or Mac with a saved macro-enabled workbook (.xlsm or .xlsb) and access to the VBA Editor.
  • Your function written inside a standard module, not a worksheet, workbook, or class module, unless you understand how those are called.
  • Trust settings that allow macros to run — check Trust Center > Macro Settings if the editor refuses to run anything.
  • A small wrapper Sub that supplies test arguments so you can call the function on demand.

Step-by-Step

  1. Unprotect the project if needed. Open the VBA Editor with Alt+F11 (Windows) or Option+F11 (Mac). If the project is password-protected, you must unlock it first: a locked project hides all code and breakpoints cannot be set.
  2. Confirm the code is in a standard module. In the Project Explorer (View > Project Explorer), the function should sit under a module listed under Modules. Worksheet code modules run only on their own sheet events, which makes them hard to call directly.
  3. Add Option Explicit to the top of the module. This forces declaration of every variable and catches typos like lRow versus lrow at compile time rather than at runtime. Run Debug > Compile VBAProject afterward; a compile error points you to a syntax problem before you even execute.
  4. Write a wrapper Sub. Below the function, add something like:
Sub TestMyFunction()
    Dim result As Variant
    result = MyFunction(5, "abc")
    Debug.Print result
End Sub

Place the cursor inside this Sub and press F5 to run it. This is the only reliable way to debug a function that normally lives in a cell formula.

  1. Set a breakpoint. Click in the grey margin beside the first line you want to inspect, or place the cursor there and press F9. A red dot and highlighted line appear. Breakpoints are cleared whenever you stop the project or close the workbook without saving, so set them after any reset.
  2. Run the wrapper. Press F5 or choose Run > Run Sub/UserForm. Execution stops at the red line and that line turns yellow. The yellow line has not yet executed.
  3. Step through line by line. Press F8 (Step Into) to execute one line at a time. Use Shift+F8 (Step Over) to run a whole called procedure without entering it, and Ctrl+Shift+F8 (Step Out) to finish the current procedure and return to the caller.
  4. Inspect variables. Hover over a variable to see its current value in a tooltip, or open the Immediate Window with Ctrl+G and type ?variableName followed by Enter. Type ?MyFunction(5,"abc") directly in the Immediate Window to call the function from any point while the project is paused, as long as the code is not currently stopped inside that same function (that causes a re-entrancy error).
  5. Use the Locals and Watch windows. View > Locals Window shows every variable in scope and its value; View > Watch Window lets you add an expression such as Len(myString) > 0 and see it evaluated as you step.
  6. Check argument types at the boundary. The most frequent function bugs are type mismatches: a cell value arriving as an empty Variant, a number arriving as text, or an error value like #N/A being passed in. In the Immediate Window type ?VarType(argName) to see what you actually received. VBA type codes are numbers: 2 is Integer, 3 is Long, 5 is Double, 8 is String, 12 is Variant, 10 is Error.
  7. Test the output before returning it. Step to the line just before End Function and print the value you are about to assign to the function name. If the wrong value is being returned, the bug is inside the function; if the right value is returned but the cell shows something else, the problem is the calling formula or the cell format.
  8. Handle errors deliberately. If the function exits silently, check whether an On Error Resume Next in the caller is swallowing the error. In the Immediate Window, type ?Err.Number and ?Err.Description after the call to see what happened. Remove blanket On Error Resume Next while debugging.
  9. Clear breakpoints and reset. Choose Debug > Clear All Breakpoints (Ctrl+Shift+F9) and press the Reset button (the blue square) when finished, so leftover breakpoints do not surprise you later.

Common Mistakes

  • Expecting a breakpoint to fire inside a cell formula. Breakpoints in functions called from a worksheet formula do not stop execution in normal calculation. Always call the function from a wrapper Sub.
  • Forgetting to reset. After an error, the project may still be in break mode; running again without pressing Reset can cause odd behaviour or a "code execution has been interrupted" prompt.
  • Declaring variables with the wrong type. Dim x As Integer overflows at 32,767; use Long for row numbers and counts.
  • Watching the wrong variable. The Locals window shows the current procedure only. If you stepped out, the variables you expect may no longer exist.
  • Editing code while paused. Modifying a running procedure can force VBA to restart the project and lose your state. Edit after resetting.

Tips

  • Press F8 twice on the first line of a Function when it is called from a Sub; the first press enters the function, later presses walk it.
  • Use Debug.Print statements liberally — they cost nothing and show up in the Immediate Window without pausing execution.
  • Add Stop as a temporary statement if you cannot click a margin, and delete it afterward.
  • Name the function result explicitly, e.g. MyFunction = total, and calculate into a local variable first so you can inspect it.
  • Test with deliberately awkward inputs: empty cell, zero, negative number, text, and an error value.

Troubleshooting

The breakpoint disappears when I click elsewhere. The line is not executable (a declaration, comment, or blank line). Click on an actual statement.

I get "Compile error: Expected: expression". A syntax problem exists somewhere in the module. Fix it before running; VBA cannot execute partially broken code.

F8 steps into a built-in function. Use Shift+F8 to step over instead. Built-in functions have no VBA code to inspect.

Excel crashes or freezes at a breakpoint. Save the workbook, close other workbooks, and disable add-ins temporarily via File > Options > Add-ins. A misbehaving add-in can interfere with the editor.

The function returns #VALUE! in the cell. Confirm the arguments are the right types by calling the function in the Immediate Window with the same values. If it works there, the cell reference is passing something different, such as an error or blank.

Frequently Asked Questions

Why can't I set a breakpoint in my function?

Usually because the line is not executable or the project is locked. Unlock the VBA project and click an actual statement line, not a declaration or blank line.

How do I debug a function called only from a worksheet cell?

Create a temporary Sub that calls the function with the same arguments and run that. Worksheet-formula calls bypass breakpoints during normal calculation.

What is the Immediate Window shortcut?

Ctrl+G. If it is hidden, open it via View > Immediate Window.

Can I see the value of an array while stepping?

The Locals window shows the array in a tree; expand it to inspect elements. For large arrays, print specific elements with ?arr(3) instead.

Why does the function work in one workbook but fail in another?

Likely references or macros are disabled, or the other workbook lacks the same functions. Check Tools > References in the VBA Editor and the Trust Center macro settings.