Excel VBA Exit Function: When and How to Use It Safely

VBA & macros · Updated November 21, 2026

In Excel VBA, Exit Function stops the current function immediately and returns control to the statement that called it. Before using it, assign the function’s return value if you need a particular result; otherwise VBA returns the type’s default value.

What Exit Function Means

A VBA function performs a task and provides a value to its caller. Exit Function is a control-flow statement that ends that function before execution reaches its final End Function line. It does not close Excel, stop every running procedure, or end the workbook. It only leaves the function in which the statement appears.

This is especially useful when a function discovers that it cannot or should not continue. For example, it might receive an invalid input, detect a condition that needs an immediate answer, or finish a calculation in a special case. Instead of placing the rest of the code inside several layers of If statements, you can deal with the special case and exit.

A function still needs its closing End Function statement in the code. Think of Exit Function as a route out of the procedure while it is running, and End Function as the required marker that defines where the procedure ends in the editor.

How a Function Returns a Value

In VBA, a function usually sets its return value by assigning a value to its own name. For instance, inside a function named NetAmount, the statement NetAmount = Amount - Fee sets what the caller receives. VBA does not use a separate return keyword for this ordinary function-return pattern.

When Exit Function runs, VBA returns the value currently assigned to the function name. If the function has not assigned one, its result is the default for its declared return type: typically zero for a numeric type, an empty string for a String, False for a Boolean, and Nothing for an object reference. A Variant that has not been assigned is generally Empty. Relying on these defaults can make a bug hard to spot, so choose an intentional return value whenever possible.

The return type is specified at the end of the function declaration, such as As Double or As Boolean. The value you assign should be compatible with that type. For an object-returning function, assign the object using Set and the function name; if the function exits without an object assignment, it returns Nothing.

A Practical Example

This function calculates an amount after a fee. It treats a negative amount as invalid for this example and exits with a deliberately assigned zero. Otherwise, it calculates and returns the result.

Public Function NetAmount(ByVal Amount As Double, ByVal Fee As Double) As Double
    ' Return zero for a negative amount.
    If Amount < 0 Then
        NetAmount = 0
        Exit Function
    End If

    ' Set the result for the ordinary case.
    NetAmount = Amount - Fee
End Function

To use this as a worksheet function, put the amount in A2 and the fee in B2, then enter =NetAmount(A2,B2) in C2. If A2 contains 100 and B2 contains 5, the result is 95. If A2 contains -10, the function returns 0 because it assigns zero before exiting. This example illustrates the control flow; in a real workbook, decide what invalid inputs should mean for your particular calculation.

Notice that the function has both an early exit and a normal path. The early path assigns a result and leaves immediately. The normal path assigns its result and then reaches End Function. This makes the intended return value visible in both cases.

Exit Function Compared with Other Exit Statements

VBA has several statements beginning with Exit, and each leaves a different kind of block. Use the one that matches the procedure or loop you intend to stop:

  • Exit Function leaves the current Function procedure.
  • Exit Sub leaves the current Sub procedure, which does not return a function value.
  • Exit For leaves the nearest active For or For Each loop, then continues after that loop.
  • Exit Do leaves the nearest active Do loop.
  • Exit Property leaves the current Property procedure.

If a function contains a loop, Exit Function ends the whole function, not just the loop. Use Exit For when you only want to stop searching and then perform more work after the loop. Conversely, if finding a match means the function’s answer is already known, exiting the function can be clearer.

Do not confuse Exit Function with the standalone statement End. End abruptly stops VBA execution and resets module-level variables; it is not a normal way to leave a function. Use a structured exit statement and let the calling procedure continue normally.

Using It with Conditions, Loops, and Errors

An early exit is useful for validation. A function that requires a positive value might check that condition first and return a clear value for invalid input. This pattern is often called a guard clause: handle a case that rules out further processing near the beginning, then keep the main calculation less indented.

It can also be appropriate inside a loop. For example, a function that searches a range may return as soon as it finds the first qualifying item. If it is meant to return a count, however, exiting on the first match would produce the wrong result. Choose based on the function’s purpose, not just on the fact that an exit statement is available.

Error handling needs special attention. If a function uses On Error GoTo and has an error-handling label near the bottom, a normal path should usually include Exit Function before that label. Otherwise, ordinary execution can continue into the error-handling section even though no error occurred. The exit keeps the normal path separate from the handler. In functions that acquire resources or must perform cleanup, make sure every exit path still carries out any required cleanup before leaving.

In a worksheet user-defined function, prefer returning a value rather than changing other cells or controlling Excel’s interface. A formula-based function is expected to calculate a result; actions such as selecting cells or editing the sheet from inside a worksheet calculation can cause problems. An early exit should simply end that calculation with an appropriate result.

Choosing a Return Value for an Early Exit

The most important decision is what the caller should receive when the function exits early. A default value may look harmless but can be mistaken for a valid answer. For instance, zero might mean a real zero in a sales calculation, while an empty string might be displayed as if a record were intentionally blank.

Consider these options, depending on the function’s contract:

  • Return a defined value such as False when the function answers a yes-or-no question.
  • Return a documented sentinel value only when callers can reliably distinguish it from valid results.
  • Return a suitable Excel error value from a worksheet UDF when the input makes a calculation invalid and displaying an error is more useful than silently returning zero.
  • Raise or handle an error when the condition represents a programming or operational failure rather than an ordinary input case.

Be consistent. If one invalid condition returns zero, another returns an empty string, and a third falls through to a type default, users and calling code have to guess what each result means. Document the expected result and, when relevant, validate inputs at the boundary of the function.

Common Mistakes and Troubleshooting

A frequent mistake is writing Exit Function before assigning the result. The code compiles, but it may return zero, False, an empty string, or another default rather than the intended answer. When a function unexpectedly produces a blank or zero, inspect every early-exit branch and confirm that it sets the function name first.

Another mistake is using Exit Sub inside a Function. VBA will report a compile error because the statement does not match the kind of procedure. Likewise, Exit Function does not belong in a Sub. Check the declaration at the top of the procedure when VBA highlights an exit statement.

Exiting a loop is not the same as exiting the function. If a search stops but the function then returns an uninitialized value, the code may need to assign the answer before leaving the loop or assign a fallback after the loop. Trace what happens after each exit: does execution continue in the function, or does it return to the caller?

Finally, do not remove End Function because an early exit is present. The exit statement is executed only when its path is reached; the procedure still needs a valid definition in the editor. Also remember that macro actions generally cannot be reversed with Ctrl+Z. Save a copy of the workbook before running VBA code that changes data.

Where to Put and Run VBA Code

In Excel for Microsoft 365 on Windows, open the Visual Basic Editor with Alt+F11. If the Developer tab is not visible, enable it through File > Options > Customize Ribbon and select Developer. In the editor, choose Insert > Module, then place a standard function in that module. Save a workbook containing VBA as an Excel Macro-Enabled Workbook (.xlsm) so the code can be retained.

On a Mac, Excel also supports VBA, though some VBA features and object-model behavior differ from Windows. The Visual Basic Editor shortcut is commonly Option+F11; on some keyboards you may also need Fn. Excel for the web does not create, edit, or run VBA macros, so open the workbook in the desktop application to work with this code. Before running macros that modify workbook contents, keep a separate copy because Ctrl+Z cannot undo their changes.

Frequently Asked Questions

Does Exit Function return a value?
Yes. It returns the value currently assigned to the function name. If no value has been assigned, VBA supplies the default for the function’s return type, which may not be the result you intended.

Do I still need End Function after Exit Function?
Yes. End Function marks the end of the function’s code in the editor. Exit Function is an optional statement that leaves the procedure early when execution reaches it.

Can I use Exit Function inside an If statement or loop?
Yes. It can appear inside conditional code or a loop, and it leaves the whole function from that point. Use Exit For or Exit Do instead if you intend to leave only the loop.

What is the difference between Exit Function and End?
Exit Function leaves one function and returns control to its caller. The standalone End statement abruptly stops VBA execution and resets module-level variables, so it is not a substitute for a normal function exit.

Can a worksheet formula call a VBA function that uses Exit Function?
Yes. A worksheet formula can call a public function in a standard module, and that function can exit early. The function should return a meaningful value and avoid trying to edit cells as part of a worksheet calculation.

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.