The IsNumeric function in Excel VBA is a built-in tool that determines whether a specific value can be interpreted as a number. It returns a Boolean result—True if the data is numeric, and False if it is text or an error. This check is essential for preventing runtime errors when performing calculations or assigning values to numeric variables. Before using this function, ensure you have access to the Visual Basic Editor (VBE) within your Excel workbook.
What You'll Need
- Microsoft Excel installed on your computer (desktop version).
- A workbook containing data with mixed types (numbers and text).
- Basic familiarity with opening the VBA Editor via Alt + F11.
Step-by-Step
- Open the Visual Basic Editor: Press
Alt + F11while in Excel to launch the VBE. If the Project Explorer window is not visible, pressCtrl + Rto display it. - Insert a Module: In the Project Explorer, right-click on your workbook name (e.g.,
VBAProject (Book1.xlsx)). Select Insert > Module. This creates a blank code window where you can write your subroutine. - Define Your Subroutine: Type
Sub CheckIfNumber()and press Enter. End the block later withEnd Sub. This structure holds the logic for checking cell values. - Select the Target Cell: Inside the subroutine, define a range variable or directly reference a cell. For example, type
Dim targetCell As Rangefollowed bySet targetCell = Range("A1"). This tells VBA which cell to inspect. - Apply the IsNumeric Function: Write an
Ifstatement using the function. TypeIf IsNumeric(targetCell.Value) Then. The.Valueproperty ensures you are checking the actual content of the cell rather than the object itself. - Add Action Code: On the next line after the
Thenkeyword, add what should happen if the value is numeric. For demonstration, useMsgBox "The value is a number.". - Handle Non-Numeric Cases: Add an
Elseblock. TypeElseand thenMsgBox "The value is not a number.". This covers scenarios where the cell contains text, blanks, or errors. - Close the Block: Type
End Ifto close the conditional statement, followed byEnd Subto finish the macro. - Run the Macro: Place your cursor inside the subroutine and press
F5, or go to the Run menu and select Run Sub/UserForm. A message box will appear indicating the result based on the current value in cell A1.
Common Mistakes
One frequent error is forgetting to specify the .Value property. While IsNumeric(Range("A1")) often works because VBA implicitly evaluates the default property, explicitly writing Range("A1").Value is safer and clearer. Another mistake is assuming IsNumeric validates mathematical correctness. It only checks if the string representation looks like a number. For instance, it might return True for strings like "1E+05" (scientific notation) or hex numbers like "&HFF", which may not be intended depending on your business logic.
Users also frequently confuse IsNumeric with VarType. IsNumeric is a quick boolean check, whereas VarType returns an integer representing the exact data type (e.g., Double, String, Empty). Using VarType is more precise but requires more complex coding.
Tips
- Check for Blanks First: An empty cell often returns
FalseforIsNumeric. If you need to distinguish between empty cells and text, useIsEmpty()beforeIsNumeric(). - Loop Through Ranges: To check multiple cells, wrap the logic in a
For Eachloop. Example:For Each cell In Selection. This allows bulk validation of user inputs. - Use With Statement: When accessing multiple properties of the same object, use the
Withstatement to improve performance and readability. Example:With Range("A1")...End With. - Error Handling: Wrap your code in
On Error GoToblocks if you are processing large datasets, as unexpected data types can sometimes cause other operations to fail even ifIsNumericpasses.
Troubleshooting
If the macro does not run, check for syntax errors highlighted in red in the VBE. Ensure all parentheses are closed and keywords like Then and End If are present. If the message box shows the wrong result, verify that the cell actually contains the expected data type. Sometimes, leading spaces in text strings (e.g., " 123") cause IsNumeric to return False. Use the Trim() function to remove whitespace before checking: IsNumeric(Trim(cell.Value)).
If nothing happens when you press F5, ensure you are running the correct module. Check the Project Explorer to confirm the active module matches where you wrote the code. Also, verify that macros are enabled in your Excel Trust Center settings.
Frequently Asked Questions
Does IsNumeric work on dates?
In many cases, yes. Excel stores dates as serial numbers. Therefore, IsNumeric(#1/1/2023#) or a cell formatted as date often returns True because the underlying value is numeric. If you need to exclude dates, check the format separately using cell.NumberFormat.
Why does IsNumeric return False for "1,000"?
This depends on your system locale settings. In some regions, commas are decimal separators, making "1,000" invalid as a number. In others, it is a valid thousands separator. However, VBA's IsNumeric generally expects standard numeric formats without localized punctuation unless specifically handled. It is best practice to clean data before testing.
Can I use IsNumeric on a variable instead of a cell?
Yes. The function accepts any variant or string input. For example, Dim myVal As Variant: myVal = "123": If IsNumeric(myVal) Then... works exactly the same way as checking a cell value.
Is there a difference between IsNumeric and CInt?
Yes. CInt attempts to convert a value to an integer. If the conversion fails, it throws a runtime error. IsNumeric simply tests if the conversion *could* happen without throwing an error. Always test with IsNumeric before attempting conversion to avoid crashes.
How do I ignore negative numbers?
IsNumeric returns True for negative numbers. To exclude them, combine the check: If IsNumeric(val) And val > 0 Then. Note that comparing text to a number might require careful handling to avoid type mismatch errors, so ensure the value is confirmed numeric first.