When working with table data in Excel VBA, there are often times when you need to verify “whether a specific cell contains a number.”
For example, you might want to exclude non-numeric input before performing calculations.
In this article, I will explain how to determine if a cell’s value is numeric using the VBA IsNumeric function.
目次
Basic Syntax: Checking with IsNumeric Function
IsNumeric(Value)
This function determines whether the specified value can be interpreted as a number. It returns True or False.
VBA Code to Check if All Values in a Range Are Numeric
Below is an example that checks if every cell in the range “C3:E5” contains a number.
Sub CheckIfNumeric()
Dim checkCell As Range
For Each checkCell In Range("C3:E5")
If IsNumeric(checkCell.Value) = False Then
checkCell.Select
MsgBox "Non-numeric value found: " & checkCell.Address
Exit Sub
End If
Next
MsgBox "All cells contain numbers."
End Sub
Explanation
Range("C3:E5"): Specifies the range of cells to check.IsNumeric(checkCell.Value): ReturnsFalseif the cell value is not a number.- The code highlights the first non-numeric cell found (
.Select) and displays an alert. - If all cells are numeric, a completion message is displayed.
IsNumeric Function Evaluation Examples
Here is how IsNumeric evaluates different types of input:
| Input Value | IsNumeric Result | Remarks |
123 | True | Number |
"456" | True | Numeric strings are also valid |
"abc" | False | String |
"" | False | Empty cells are not numbers |
"12a" | False | Mixed content |
Example Use Cases
- Validating user input (restricting to numbers only).
- Verifying data before performing calculations that require numbers.
- Automatically detecting and notifying users of input errors.
Summary
| Action | Code Example |
| Check if a cell is numeric | IsNumeric(Range("C3").Value) |
| Check all numbers in a range | For Each loop + IsNumeric |
| Identify and select non-numeric cell | .Select + .Address |
| Display completion message | Use MsgBox |
