Return to VBA Code Examples
This tutorial will teach you how to use the IsNumeric and IsNumber functions in VBA to check if values are numbers.
IsNumeric is a built-in VBA function, while IsNumber is an Excel function which can be called from VBA code.
Difference between IsNumber and IsNumeric in VBA
IsNumber checks if a value is stored as a number. Whereas, IsNumeric checks if a value can be converted into a number.
For example, if you pass a blank cell as a parameter, IsNumber will return FALSE, while IsNumeric will return TRUE. Also, if you pass a cell containing number stored as a text, IsNumber will return FALSE and IsNumeric TRUE.
You need to pay attention to these limitations of both functions and decide in which cases is better to use IsNumeric and when IsNumber.
Using IsNumeric in VBA
IsNumeric is the VBA function which checks if a value is numeric and returns a Boolean TRUE or FALSE as a result.
The function can take a variable or a cell value.
Here is an example of taking a cell value:
If IsNumeric(Sheet1.Range("A1").Value) = True Then
MsgBox "The value in A1 is numeric"
Else
MsgBox "The value in A1 is not numeric"
End If
In this example, we check if the value from the cell A1 is numeric using the IsNumeric. This function returns the appropriate message, depending on the result of the function.
This next example perform the same operation, except with a variable instead of a cell value:
Dim n as Variant
n = Sheet1.Range("A1").Value
If IsNumeric(n) = True Then
MsgBox "The value in A1 is numeric"
Else
MsgBox "The value in A1 is not numeric"
End If
Using IsNumber in VBA
IsNumber is an Excel Function, which can be used in VBA. It has an almost similar output as IsNumeric. Let’s look at the example of the IsNumber function:
If Application.WorksheetFunction.IsNumber(Sheet1.Range("A1").Value) = True Then
MsgBox "The value in A1 is numeric"
Else
MsgBox "The value in A1 is not numeric"
End If
As you can see from the code, the difference is in the syntax when calling the function. Since IsNumber is the Excel function, we need to put Application.WorksheetFunction before the function call.
VBA Coding Made Easy
Stop searching for VBA code online. Learn more about AutoMacro — A VBA Code Builder that allows beginners to code procedures from scratch with minimal coding knowledge and with many time-saving features for all users!
Learn More!
title | keywords | f1_keywords | ms.prod | api_name | ms.assetid | ms.date | ms.localizationpriority |
---|---|---|---|---|---|---|---|
WorksheetFunction.IsNumber method (Excel) |
vbaxl10.chm137132 |
vbaxl10.chm137132 |
excel |
Excel.WorksheetFunction.IsNumber |
f2159d1b-4f56-e64e-3a08-bafbb688a683 |
05/23/2019 |
medium |
WorksheetFunction.IsNumber method (Excel)
Checks the type of value and returns True or False depending on whether the value refers to a number.
Syntax
expression.IsNumber (Arg1)
expression A variable that represents a WorksheetFunction object.
Parameters
Name | Required/Optional | Data type | Description |
---|---|---|---|
Arg1 | Required | Variant | Value — the value that you want tested. Value can be a blank (empty cell), error, logical, text, number, or reference value, or a name referring to any of these, that you want to test. |
Return value
Boolean
Remarks
The value arguments of the IS functions are not converted. For example, in most other functions where a number is required, the text value 19 is converted to the number 19. However, in the formula ISNUMBER("19")
, 19 is not converted from a text value, and the IsNumber function returns False.
The IS functions are useful in formulas for testing the outcome of a calculation. When combined with the IF function, they provide a method for locating errors in formulas.
[!includeSupport and feedback]
Sub Excel_ISNUMBER_Function()
Dim ws As Worksheet
Set ws = Worksheets(«ISNUMBER»)
ws.Range(«C5») = Application.WorksheetFunction.IsNumber(ws.Range(«B5»))
ws.Range(«C6») = Application.WorksheetFunction.IsNumber(ws.Range(«B6»))
ws.Range(«C7») = Application.WorksheetFunction.IsNumber(ws.Range(«B7»))
ws.Range(«C8») = Application.WorksheetFunction.IsNumber(ws.Range(«B8»))
ws.Range(«C9») = Application.WorksheetFunction.IsNumber(ws.Range(«B9»))
End Sub
OBJECTS
Worksheets: The Worksheets object represents all of the worksheets in a workbook, excluding chart sheets.
Range: The Range object is a representation of a single cell or a range of cells in a worksheet.
PREREQUISITES
Worksheet Name: Have a worksheet named ISNUMBER.
ADJUSTABLE PARAMETERS
Output Range: Select the output range by changing the cell references («C5») through to («C9») in the VBA code to any cell in the worksheet, that doesn’t conflict with the formula.
In VBA, to test whether an expression is a number, the IsNumeric
function can be used.
Description
The IsNumeric function evaluates whether the input expression is a number and returns a Boolean value (TRUE or FALSE). It returns True if the entire expression is a number; otherwise, it returns False.
Syntax
IsNumeric(expression)
The input expression is either a numeric expression or a string expression.
VBA examples of IsNumeric
Example 1 – Using IsNumeric with IF-THEN-ELSE
The following VBA function CheckNumeic uses IsNumeric to test whether input1 is numeric or not with an IF-THEN-ELSE statement.
Function CheckNumeric(ByVal input1) As String If IsNumeric(input1) = True Then MyFunction1 = "Input is numeric" Else MyFunction1 = "Input is not numeric" End If End Function
I would want to draw your attention to line 2. While this line is correct, it is not necessary. Instead, it can be written as:
If IsNumeric(input1) Then
In VBA, whenever you perform a logical test, for the condition of TRUE, you don’t have to type the =TRUE
in the statement.
Example2 – Negate the result of IsNumeric
The VBA function below returns the opposite of IsNumeric
. The function uses the same IF-THEN-ELSE structure.
Function IsNumericReverse(ByVal input1) As Boolean If IsNumeric(input1) = True Then IsNumericReverse = False Else IsNumericReverse = True End If End Function
However, this entire structure is not necessary. Instead, the function can be simplified as follows. The Not logical operator can be used to reverse (negate) the answer of IsNumeric, which serves the objective of the function in this case.
Function IsNumericReverse(ByVal input1) As Boolean IsNumericReverse = Not (IsNumeric(input1)) End Function
Example 3 – VBA action if not numeric
The VBA function counts the number of non-numeric cells in the input range. In line 5, note the use of Not
with IsNumeric
. In the sentence structure here, putting a space after No
t is enough, which is equivalent to using a bracket after Not
to wrap the condition being tested.
Function countNonNumeric(range1 As Range) As Long Dim cell As Range Dim counter As Long For Each cell In range1.Cells If Not IsNumeric(cell.Value) Then 'use Not to test for non-numeric counter = counter + 1 End If Next countNonNumeric = counter End Function
Special Case with Blank
When using IsNumeric, you need to be aware that “blank” (string of zero length) does not mean zero (0) and it is considered to be non-numeric:
IsNumeric("")
returns False
Therefore, when you write your macro, if you want to treat blank as numeric, you may have to use conditional statements to handle the case of blank string inputs.
Special Case with Dates
Another special case with IsNumeric
is the treatment of date inputs. Most people conceptually think dates are numeric (after all, they can be converted into date-serials in the computer). However the IsNumeric
function in VBA considers dates as non-Numeric:
IsNumeric("10/2/2020")
returns False
Even if you try to input a date truly in Date format, IsNumeric still returns False:
IsNumeric(DateSerial(2020, 10, 2))
returns False
Therefore, you may also need to use conditional statements to handle the case of dates expressions.
Special Case with Time
Handling of time expressions by the ISNUMERIC is difficult to manage, or I would describe it as unpredictable. Therefore, when you input expressions contain time expressions, you must test your macro thoroughly.
There are three possibilities with time expressions. Let’s experiment with the macro below. In cell A1, we place a time of “3:00:00 AM” first. Then we run the macro which test 3 cases:
- time in form of string (variable x1)
- time in form of time value (variable x2)
- time placed in a cell in an Excel sheet (variable x3)
Sub TimeCases() Dim y x1 = IsNumeric("3:00:00 AM") x2 = IsNumeric(TimeSerial(3, 0, 0)) y = Range("A1").Value x3 = IsNumeric(y) MsgBox x1 & Chr(10) & x2 & Chr(10) & x3 End Sub
Run the macro and the answers will be displayed in the Msgbox:
Calling Excel Worksheet Function ISNUMBER() in VBA
As an alternative to IsNumeric
in VBA, you may call the Excel worksheet function ISNUMBER()
in your macro. There are two ways to do this. See line 2 and line 3 in the VBA function below, which do the same job. (I personally prefer Method 2.)
Function CallIsNumber(input1) As Boolean x = WorksheetFunction.IsNumber(input1) 'Method 1' x = Application.IsNumber(input1) 'Method 2' CallIsNumber = x End Function
VBA ISNUMERIC vs ISNUMBER Worksheet Function
Although you can either use IsNumeric
or Excel worksheet function ISNUMBER
to check whether an input expression is numeric, you have to be aware of the differences between the two methods in order to program your macros correctly to produce expected result.
Expression | Date type of expression | ISNUMERIC returns (VBA) | ISNUMBER returns (worksheet function) |
123 | Number | TRUE | TRUE |
“123” | Number in form of string | TRUE | FALSE |
12/2/2020 | Date | FALSE | TRUE |
“12/2/2020” | Date in form of String | FALSE | FALSE |
DateSerial(2020,10,2) | Date in form of Date Value | FALSE | FALSE |
3:00:00 AM | Time placed in a cell | TRUE | TRUE |
“3:00:00 AM” | Time in form of String | FALSE | FALSE |
TimeSerial(3, 0,0) | Time in form of Time Value | FALSE | FALSE |
“” (blank) | String | FALSE | TRUE |
See also:
- Else if in VBA
IsNumeric Vs IsNumber in VBA
The first difference between these two functions is:
IsNumeric is the VBA function that you may use directly.
IsNumber is the Excel function that you may use in VBA. However, you have to use this with Application.WorksheetFunction object.
What do both function check?
The IsNumeric function checks if an expression can be evaluated as a number.
On the other hand, IsNumber checks if the given expression is a number or not.
Both return a Boolean value i.e. True/False
We will show their potential usage with examples.
An example that will make it clear
Just look at this simple example to learn how both functions work.
We have a String variable in a VBA program and assign it the following value:
str = “15”
Now, let us pass this string in both functions and see the output:
Sub IsNum_ex() Dim str As String str = «15» MsgBox «IsNumber Result: « & Application.WorksheetFunction.IsNumber(str) MsgBox «IsNumeric Result: « & IsNumeric(str) End Sub |
Results:
IsNumber output:
IsNumeric output:
Now let us look at a few more examples to understand the difference.
What if we pass the Integer variable to both functions
We declared an integer variable now and passed this to IsNumber and IsNumeric functions. See the output for both:
Sub IsNum_ex() Dim num As Integer num = 10 MsgBox «IsNumber Result: « & Application.WorksheetFunction.IsNumber(num) MsgBox «IsNumeric Result: « & IsNumeric(num) End Sub |
Result:
What about using an empty Excel cell in IsNumber and IsNumeric?
Sub IsNum_ex() MsgBox «Empty Cell IsNumber Result: « & Application.WorksheetFunction.IsNumber(Range(«A3»)) MsgBox «Empty Cell IsNumeric Result: « & IsNumeric(Range(«A3»)) End Sub |
Result:
Using an InputBox example
In the example below, we take the user input by using the InputBox function.
Entered a number in the input box and let us see what both functions return:
Sub IsNum_ex() Dim x x = InputBox(«Enter a number») MsgBox «User Input IsNumber Result: « & Application.WorksheetFunction.IsNumber(x) MsgBox «User Input IsNumeric Result: « & IsNumeric(x) End Sub |
Result:
You saw we entered 450 in the input box and isNumber returned False whereas IsNumeric returned True as this value can be converted to a number.
If we enter an alphabet, both functions will return False.
Also note, x is declared as a Variant. If we declared x as an integer then it results in True for 450 number (for both functions).
Conclusion
The IsNumber function is used to check if a given expression (number, variable, cell, etc.) is a number or not.
It returns True if it’s a number. For a string (with a number), empty cell, or text – it returns False.
The IsNumeric function is used to test if the given expression can be evaluated as a number.
If a string contains numbers, it can be converted to an integer – so you may want to check before performing conversion or using a suspected string in the calculation.