[VBA] How to Check if a Cell Value is Numeric Using the IsNumeric Function

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): Returns False if 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 ValueIsNumeric ResultRemarks
123TrueNumber
"456"TrueNumeric strings are also valid
"abc"FalseString
""FalseEmpty cells are not numbers
"12a"FalseMixed 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

ActionCode Example
Check if a cell is numericIsNumeric(Range("C3").Value)
Check all numbers in a rangeFor Each loop + IsNumeric
Identify and select non-numeric cell.Select + .Address
Display completion messageUse MsgBox
よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次