Excel VBA IsNumber: How to Use It Correctly
In this article
In Excel VBA, the quick way to check whether a value is a number is If IsNumeric(value) Then. Note the name: VBA's built-in function is IsNumeric, not IsNumber (that is a worksheet function name). If you specifically need Excel's own ISNUMBER logic — for example to test the result of a formula — you can call it through Application.WorksheetFunction.IsNumber(...). This guide shows both, when to use each, and how to avoid the classic pitfalls.
What You'll Need
- Excel (any modern version on Windows or Mac) with a workbook to test in
- The Developer tab visible, or the Alt+F11 shortcut to open the VBA editor
- Macros enabled so you can run your test code
Step-by-Step
Open the VBA editor. Press Alt+F11 (or on Mac, Tools > Macro > Visual Basic Editor; labels can differ slightly by version).
Insert a module. In the editor, choose Insert > Module. A blank code window opens.
Use IsNumeric in an If statement. Type a simple test macro:
Sub TestIfNumber() Dim v As Variant v = Range("A1").Value If IsNumeric(v) Then MsgBox "A1 is a number" Else MsgBox "A1 is not a number" End If End SubRun it. Put a value in cell A1, click anywhere inside the macro, and press F5. Change A1 to text and run again to see the Else branch fire.
Negate the check when needed. To act on non-numbers, use
If Not IsNumeric(v) Then. You can chain withElseIffor multiple conditions.Check the whole cell, including formulas, with WorksheetFunction.
IsNumericonly sees the value VBA reads. If you want the exact behaviour of the worksheet function ISNUMBER, write:If Application.WorksheetFunction.IsNumber(Range("A1")) ThenThis also works on a multi-cell range and returns True/False per the worksheet function's rules.
Combine with other checks for robust code. For example, require both a number and an non-empty cell:
If IsNumeric(v) And Not IsEmpty(v) ThenUse it in a loop. A typical pattern for scanning a column:
Dim r As Range For Each r In Range("A1:A100") If IsNumeric(r.Value) And r.Value <> "" Then ' process the number End If Next rSave your work. Save the workbook as a macro-enabled file (.xlsm) if you want to keep the code.
Common Mistakes
- Typing
IsNumberinstead ofIsNumeric. PlainIsNumber(x)does not exist in VBA and causes a compile error. UseIsNumeric, orApplication.WorksheetFunction.IsNumberif you truly want the worksheet function. - Assuming IsNumeric guarantees a pure number. It returns True for numeric-looking strings such as "123" and "1e5", and even "&H1A" in some cases. It answers "can this be evaluated as a number?", not "is this stored as a number?". If you need the latter, also test
VarType(v) = vbDoubleor similar. - Forgetting that empty cells are numeric.
IsNumeric("")returns False, but an empty Variant read from a blank cell can behave unexpectedly in comparisons. Add anIsEmptyor= ""check when blanks matter. - Dates and currency. Dates stored as dates are numeric to VBA (they are serial numbers), and cells formatted as Currency usually evaluate as numeric. Decide explicitly how to treat them.
- Not handling errors from WorksheetFunction. Some worksheet functions throw errors when misused;
IsNumberis safe with valid ranges, but passing invalid arguments raises a runtime error rather than returning False.
Tips
- Read the value once into a Variant.
Dim v As Variant: v = Range("A1").Valueis faster in loops than repeated range reads. - Prefer
.Value2for pure number work. It reads dates and currency as plain doubles, simplifying type checks. - Use
IsNumericbeforeCDblorCLngto avoid type mismatch errors when converting strings. - For strict "is it stored as a number" tests in a cell, you can also use
VarType(Range("A1").Value)and compare againstvbDouble,vbInteger,vbLong,vbSingle,vbCurrencyorvbDate. - Comment your intent. Note whether the check means "numeric-looking" or "stored as a number" so future-you understands the logic.
Troubleshooting
- Compile error: Sub or Function not defined on IsNumber. You typed the worksheet function name directly in VBA. Replace it with
IsNumericor prefix it withApplication.WorksheetFunction.. - The macro never runs / macros are disabled. Check the message bar under the ribbon for a security warning and enable macros for this workbook only, then press F5 again.
- Text like "42" passes the check unexpectedly. That is normal for IsNumeric. Add a
VarTypetest if you must exclude numeric strings. - Error 1004 or 438 from WorksheetFunction. Make sure you passed a valid Range object and spelled
Application.WorksheetFunction.IsNumbercorrectly. If unsure, revert toIsNumericonRange.Value, which covers most cases. - Code works on one sheet but not another. Unqualified
Range("A1")refers to the active sheet. Fully qualify it:Worksheets("Sheet1").Range("A1").
Frequently Asked Questions
Is there an IsNumber function in VBA?
Not as a direct VBA function. VBA's built-in test is IsNumeric. Excel's worksheet function ISNUMBER is available only via Application.WorksheetFunction.IsNumber(...).
What's the difference between IsNumeric and ISNUMBER?
IsNumeric evaluates whether an expression can be converted to a number, so "123" returns True. The worksheet ISNUMBER checks whether a value is actually stored as a number, so "123" as text returns False.
Does IsNumeric return True for dates?
Yes. VBA stores dates as serial numbers, so a real date value is numeric. Test with IsDate first if you want to treat dates separately.
How do I check if a whole range contains only numbers?
Loop through the cells with For Each and test each with IsNumeric, or use Application.WorksheetFunction.Count on the range and compare it to the count of non-empty cells.
Can I use IsNumeric on a cell directly?
Yes, e.g. IsNumeric(Range("A1")) works because the default property of a Range is Value, but writing Range("A1").Value explicitly is clearer and safer.
Why does my If statement say an empty cell is not numeric?
An empty cell reads as Empty, and IsNumeric on an empty string returns False. Handle blanks explicitly with IsEmpty(v) or v = "" before testing.