VBA IsNumeric
IsNumber an excel function is used for identifying whether the cell content is a number or not. A numeric value can be a whole value or integer. An IsNumeric function can also be performed in VBA as well. In VBA this is available with the name “IsNumeric“. IsNumeric works in the same manner as IsNumber does. It analyzes the cell value and returns the answer whether it is a number or not.
IsNumeric considers only Boolean, which only gives result in the form of TRUE and FALSE.
Syntax of IsNumeric in Excel VBA
VBA IsNumeric has the following syntax in VBA:
How to Use Excel VBA IsNumeric?
We will learn how to use a VBA IsNumeric with few examples in excel.
VBA IsNumeric – Example #1
Let’s see an easy example where we will select a cell with any content and in a separate cell, we will see whether cell content is a Number or Not.
Step 1: For this open, a new module in the VBA window under the Insert menu tab as shown below.
Step 2: Write a Subcategory in the name of a performed function or in any other name as shown below.
Code:
Sub VBA_Isnumeric1() End Sub
Step 3: Now we will use the If-Else loop for this complete condition. For this open and close If bracket as shown below.
Code:
Sub VBA_Isnumeric1() If End If End Sub
Step 4: Now in If write and select IsNumeric function and select any Range cell from where we will analyze the content. Here we have selected cell A1 as TRUE.
Code:
Sub VBA_Isnumeric1() If IsNumeric(Range("A1")) = True Then End If End Sub
Step 5: If cell value at cell A1 is TRUE it means it is a number then we can choose writing any sentence in cell B1 which will say “It is a Number” or put any text as per your choice.
Code:
Sub VBA_Isnumeric1() If IsNumeric(Range("A1")) = True Then Range("B1") = "It is a Number" End If End Sub
Step 6: Now in Else line of code consider writing what we could see when IF condition doesn’t work. We are selecting cell B1 where we will see the output statement of cell A1 as “It is not a Number” as shown below.
Code:
Sub VBA_Isnumeric1() If IsNumeric(Range("A1")) = True Then Range("B1") = "It is a Number" Else Range("B1") = "It is not a Number" End If End Sub
Step 7: Once done then compile and run the complete code. As we can see in the below screenshot for the cell content A1 we got the statement as “It is a Number” in cell B1.
Step 8: Now let’s replace 10 in cell A1 with a text like “Test” and see what we get.
Step 9: Now again run the complete code.
As we can see in the above screenshot, for the cell content A1 we got the statement as “It is not a number” for content “Test” which means cell A1 doesn’t have a number in it.
VBA IsNumeric – Example #2
There is another way to add IsNumeric. By far we all know that Boolean function is used for TRUE/ FALSE on the basis of what we feed and define the condition. Here we will use Boolean to calculate IsNumeric for any content of the cell.
Step 1: Write a Subcategory in the name of a performed function as shown below.
Code:
Sub VBA_IsNumeric2() End Sub
Step 2: Now define a dimension “DIM” as A and assign it to Double. We can assign it as Integer or Long too. But that would only consider whole numbers and long text/numbers. Double is used where we are expecting to get numbers in decimal forms.
Code:
Sub VBA_IsNumeric2() Dim A As Double End Sub
Step 3: Now define one more dimension “DIM” as X. And assign it as Boolean. We can consider any word, name or alphabet for defining dimensions in VBA.
Code:
Sub VBA_IsNumeric2() Dim A As Double Dim X As Boolean End Sub
Step 4: Now for Dim A double, first assign the value as 10 which is a whole number.
Code:
Sub VBA_IsNumeric2() Dim A As Double Dim X As Boolean A = 10 End Sub
Step 5: Now for Boolean X, use IsNumeric function and assign defined Double A into the brackets of IsNumeric. By doing this IsNumeric will fetch the value stored in Dim A and analyze whether that value is a number or not.
Code:
Sub VBA_IsNumeric2() Dim A As Double Dim X As Boolean A = 10 X = IsNumeric(A) End Sub
Step 6: To get the answer of an analysis done by Isnumeric, we will assign it to a message box where we will see the result.
Code:
Sub VBA_IsNumeric2() Dim A As Double Dim X As Boolean A = 10 X = IsNumeric(A) MsgBox "The expression(10) is numeric or not : " & X, vbInformation, "VBA IsNumeric Function" End Sub
Step 7: Once done compile and run the code.
As we can see a pop-up dialog box in the above image, the expression(10) is a TRUE numeric value.
Step 8: Now let’s change the value and put some decimal value in IsNumeric as shown below and see what output we get. Here we have to change the value of A as 10.12 and updated the message box with expression(10.12).
Code:
Sub VBA_IsNumeric2() Dim A As Double Dim X As Boolean A = 10.12 X = IsNumeric(A) MsgBox "The expression(10.12) is numeric or not : " & X, vbInformation, "VBA IsNumeric Function" End Sub
Step 9: Now again compile and run the complete code.
We will again get TRUE for the value 10.12 which is a decimal value.
Step 10: Now let’s see if the current defined syntax of IsNumeric still works for other than numbers or not. For this, we need to change the Dim A as String which means we will be entering the Text value here. And change value entered for A. We have considered sample value as “ABC”. Make all necessary changes in related fields where we can place text instead of numbers and keep the rest of the things as it is.
Code:
Sub VBA_IsNumeric2() Dim A As String Dim X As Boolean A = ABC X = IsNumeric(A) MsgBox "The expression('ABC') is numeric or not : " & X, vbInformation, "VBA IsNumeric Function" End Sub
Step 11: After that compile and run the complete code.
Pros of Excel VBA IsNumeric
- It is so easy to apply IsNumeric in VBA. It is as simple as applying Isnumber through insert function.
- IsNumeric and IsNumber give the same result.
Cons of Excel VBA IsNumeric
- Applying example-2 where we need to insert text makes the simple process lengthy.
Things To Remember
- Recording a macro is also a way to perform IsNumeric function in VBA.
- Save the file in Macro-Enabled Excel, This is the best way to avoid losing written code.
- Best way to avoid any error while running the code is to first compile the complete code before we fix it as final.
- Assigning the created code into a button is also a way to perform created macro quickly, this saves time.
- If you are applying IsNumeric by example-2 method then remember to keep text in the single quote (‘Text’) otherwise it will give an error.
- As IsNumeric in VBA is used, in excel it used in the name of IsNumber.
Recommended Articles
This has been a guide to VBA IsNumeric. Here we discussed how to use Excel VBA IsNumeric along with practical examples and downloadable excel template. You can also go through our other suggested articles –