VBA Function As Boolean: How to Return True or False

VBA & macros · Updated November 6, 2026

Yes. In VBA, declare a function with As Boolean when it should return either True or False, then assign the result to the function’s name. For example, Public Function IsAdult(ByVal age As Long) As Boolean can return the result of a comparison such as age >= 18.

What As Boolean Means in a Function Declaration

VBA uses the words after As in a function declaration to specify the function’s return type. In Function IsAdult(age As Long) As Boolean, IsAdult is the function name, age As Long defines its parameter, and the final As Boolean says that the function returns a Boolean value.

A Boolean has just two possible values: True and False. This makes it a useful return type for questions that have a yes-or-no answer: whether a cell is blank, a number is within a permitted range, or a record meets a condition. Choosing Boolean communicates that callers should expect a logical result, rather than text, a number, or a worksheet range.

VBA stores Boolean values using zero for False and -1 for True. You will usually see the words True and False in VBA and Excel, not those underlying numbers. The representation matters mainly when older code mixes Boolean values with numeric calculations; avoid relying on that behavior when a clear comparison will do.

How a VBA Function Returns a Boolean

VBA does not use a return statement to deliver a function result. Instead, assign the result to the function’s own name. In a function called IsAdult, the assignment is IsAdult = age >= 18. The comparison itself evaluates to True or False, so it can be assigned directly.

A function’s Boolean result starts as False if you have not assigned it another value. That default can be convenient, but it can also hide a missing assignment: a function may quietly return False even when the intended logic was never reached. Make the normal result explicit, and make sure every branch that should return True assigns it.

You can also leave a function early with Exit Function. Before doing so, assign the appropriate result; otherwise, the function returns whatever value it currently holds. For functions with several branches, returning early can make the conditions easier to read, but a single final assignment can be clearer when the logic is simple.

A Complete Boolean Function Example

This example answers whether an age meets a threshold. It uses a Long parameter for a whole-number age and returns the result of a comparison:

Public Function IsAdult(ByVal age As Long) As Boolean
    ' True when age is 18 or older.
    IsAdult = (age >= 18)
End Function

For this function, an input of 17 returns False, while an input of 18 returns True. Parentheses around the comparison are optional here, but they make it visually obvious that the expression supplies the Boolean result. ByVal means the function receives a copy of the argument; it does not change the caller’s variable.

To try it inside VBA, place the function in a standard module. In Excel for Microsoft 365 on Windows, press Alt+F11 to open the Visual Basic Editor, then choose Insert > Module. On Mac, the editor shortcut is commonly Option+F11, sometimes with the Fn key; the Tools menu in Excel also provides access to the Visual Basic Editor. The exact shortcut can depend on your keyboard settings.

You can call the function from another VBA procedure, for example by storing its result in a declared Boolean variable. A test procedure can display the outcome in the Immediate window or a message box. If you run a macro that changes workbook content, save a copy first: macro actions cannot be undone with Ctrl+Z.

Calling the Function in VBA and Excel

In VBA, a Boolean function can be used directly in an If condition. For example, code can check If IsAdult(age) Then and run one branch when the answer is True. You do not need to compare the function result with the word True; the function already supplies a Boolean value. An explicit comparison is occasionally useful for teaching or debugging, but it is usually redundant.

When a function is in a standard module and declared Public, Excel can often use it as a user-defined function in a worksheet formula. With the example above, entering =IsAdult(18) in a cell returns TRUE. The workbook must support macros, such as the .xlsm format, and the user must permit the workbook’s macros to run. If Excel blocks macros, the worksheet function will not be available as expected.

Worksheet formulas and VBA calls are different contexts. A VBA caller can store the result in a Boolean variable and make decisions with it. In a worksheet, Excel displays the logical result as TRUE or FALSE, and other formulas can use that result in logical tests. A user-defined function is generally intended to calculate and return a value, not to edit other cells as a side effect.

Choosing Parameters and Handling Inputs

The function’s return type and its parameter types solve different problems. As Boolean after the closing parenthesis specifies what the function returns; age As Long inside the parentheses specifies the kind of input it accepts. A function can accept a number, text, a Boolean, or another suitable type and still return Boolean.

Use a parameter type that matches the data the function is meant to receive. For a whole-number count, Long is often a sensible choice. For a yes-or-no option supplied by another procedure, a Boolean parameter may be appropriate. If worksheet users might pass a blank cell, an error value, or mixed data, decide deliberately whether to accept a Variant and validate it or require a narrower type.

Be cautious with implicit conversions. VBA can convert some numeric values to Boolean: zero becomes False, and a nonzero number becomes True. That does not mean a value like 2 is a clear substitute for a deliberate yes-or-no input. Use comparisons such as amount > 0 when the intended meaning is “greater than zero,” and use CBool only when converting a value is genuinely part of the design. Invalid conversions can raise a run-time error.

Common Mistakes and How to Avoid Them

  • Leaving off the return type: Function IsAdult(age As Long) does not explicitly declare a Boolean result. Add As Boolean after the parameter list so the contract is clear.
  • Assigning to the wrong name: Inside IsAdult, write IsAdult = .... Assigning to a similarly named variable does not set the function’s result.
  • Using text instead of a Boolean: Returning the string "True" is not the same as returning the Boolean value True. Text can create conversion surprises when callers expect a logical value.
  • Forgetting a branch: If only one condition assigns True and another intended condition is overlooked, the function may return its default False. Review every branch, including early exits.
  • Treating an error as an ordinary False answer: A calculation can fail before the function assigns a result. Decide whether an error should be raised to the caller or handled explicitly; silently converting every error to False can make a real problem look like a valid negative answer.

A Boolean function should describe the condition it evaluates, not conceal unrelated work. Names such as IsInRange, HasValue, and CanSubmit tell a caller what True means. A vague name such as Check forces the reader to inspect the implementation before the result is understandable.

Boolean Functions Compared with Other Return Types

Return Boolean when the useful answer is a decision. If callers need the reason for a decision, a Boolean alone may not carry enough information. For example, a validation function returning False tells you that a record failed, but not whether the email address was missing or the date was invalid. In that case, separate checks or a more descriptive result structure may be better.

Use a numeric return type when the function calculates a count, amount, or position, and a String when it produces text. It is possible to use a number as a truth-like value, but doing so weakens the meaning of the interface: callers must remember which numbers represent success. A declared Boolean makes the two possible outcomes explicit and lets VBA check assignments for type compatibility.

Also distinguish a Boolean-returning function from a Sub. A Sub performs an action but does not return a value that can be assigned or tested as a function result. If a procedure needs to both perform an action and report success, consider whether the action belongs in a Sub with a separate result mechanism or in a function whose primary purpose is calculating the answer.

Testing and Troubleshooting

Test both sides of the decision, especially values at the boundary. For IsAdult, useful checks include 17, 18, and 19: they confirm that the threshold is inclusive and that values immediately below it do not pass. If a function tests a range, also test its lower and upper limits and values just outside them.

In the Visual Basic Editor, use the Immediate window to evaluate a simple call, or put a breakpoint on the assignment and step through the procedure. Check the actual input value and type if the result is surprising. A worksheet cell that looks blank may contain a formula returning an empty string, for example, which is not necessarily the same as a truly empty cell.

For a worksheet function that shows #NAME?, confirm the function name, that the code is in a standard module, and that macros are enabled for the workbook. Also check that the workbook was saved in a macro-enabled format. Do not lower Excel’s security settings globally just to make an unfamiliar workbook’s function run; only enable macros for files you trust.

Frequently Asked Questions

Can a VBA function return True or False?
Yes. Add As Boolean to the function declaration and assign either a Boolean value or a comparison result to the function’s name.

How do I return False from a VBA function?
Inside a function named IsReady, use IsReady = False. You can also assign a condition that evaluates to False.

Do I need to write Return in VBA?
No. VBA returns a function result through an assignment to the function’s name. Exit Function exits the procedure, but it does not assign a result by itself.

Can a Boolean function be used in an Excel cell?
Yes, if it is an accessible user-defined function in a standard module and macros are allowed. A call such as =IsAdult(18) can display TRUE.

What does a Boolean function return if I do not assign a value?
A Boolean function’s default result is False. Do not rely on that default to handle failures; assign results intentionally and handle errors according to the function’s purpose.

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.