VBA IsNumeric Vs IsNumber

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 the Application.WorksheetFunction object.

What do both functions 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 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:

VBA isnumber

IsNumeric output:

VBA isnumeric

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:

VBA isnumber integer

VBA isnumeric integer

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:

isnumber empty cell

isnumeric empty cell

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

inputbox test

Result:

isnumber inputbox

isnumeric inputbox

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 that x is declared as a Variant. If we declare x as an integer then it results in True for the “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 a conversion or using a suspected string in the calculation.
Author - Atiq Zia

Atiq is the writer at jquery-az.com, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we solve the mysteries of coding together!