“VBA not a number” usually means your code is trying to do math on text, an empty cell, or a value Excel doesn’t recognize as numeric. The fix is to check the value with IsNumeric before using it, or to convert it with Val or CDbl. Below, you’ll find the exact causes, the code to handle them, and how to avoid the error in your macros.
What “Not a Number” Means in VBA
In everyday English, “not a number” sounds like a typo. In VBA, it usually appears in two forms:
- #VALUE! – This is an Excel worksheet error that VBA can trigger when you assign a non‑numeric string to a cell formatted as a number, or when a worksheet function like
SUMreceives text. - Type mismatch (Error 13) – This runtime error occurs when you try to perform arithmetic on a variable that contains a string like “abc” or an empty variant.
VBA itself does not have a built‑in NaN constant like JavaScript. Instead, it uses the IsNumeric function to test whether an expression can be evaluated as a number. If IsNumeric returns False, you have a “not a number” situation.
Why Does VBA See a Number as Text?
The most common reason is that the cell is formatted as Text, or the data was imported from a CSV or another system. When a cell is formatted as Text, even a digit like “123” is stored as a string. VBA reads it as a string, and arithmetic fails.
Another cause is leading apostrophes. If a user types '123 in Excel, the apostrophe is invisible but forces the cell to be text. VBA sees the apostrophe as part of the value, so IsNumeric("'123") returns False.
Empty cells are also problematic. A truly empty cell has a Variant/Empty value. In VBA, an empty variant is treated as 0 in some contexts, but if you pass it to a worksheet function like SUM, it may cause a #VALUE! error if the range includes text.
How to Check If a Value Is a Number in VBA
The core tool is the IsNumeric function. It returns True if the expression can be converted to a number, False otherwise. Here’s a simple example:
Dim myValue As Variant
myValue = Range("A1").Value
If IsNumeric(myValue) Then
MsgBox myValue * 2
Else
MsgBox "A1 is not a number"
End If
Note: IsNumeric returns True for dates (because dates are stored as numbers) and for Boolean values (True is -1, False is 0). If you want to exclude those, you need a more specific check:
If VarType(myValue) = vbDouble Or VarType(myValue) = vbInteger Or VarType(myValue) = vbLong Then
' It's a real number, not a date or boolean
End If
How to Convert Text to a Number in VBA
If you have a text value that looks like a number (e.g., “123.45”), you can convert it using Val or CDbl. The Val function stops at the first non‑numeric character, so Val("123abc") returns 123. CDbl (Convert to Double) is stricter and raises an error if the string is not a valid number.
Example:
Dim textNumber As String
textNumber = Range("B2").Text ' reads the displayed text
Dim realNumber As Double
If IsNumeric(textNumber) Then
realNumber = CDbl(textNumber)
Else
realNumber = 0
End If
For a range of cells, you can use the worksheet’s Value property and the TextToColumns method, but that’s more advanced. A simple loop works fine for small ranges:
Dim cell As Range
For Each cell In Range("A1:A10")
If IsNumeric(cell.Value) Then
cell.Value = CDbl(cell.Value)
End If
Next cell
Remember to save a copy of your workbook before running a macro that changes cell values, because macros cannot be undone with Ctrl+Z.
Common VBA Functions That Trigger #VALUE! Errors
Some worksheet functions called from VBA are picky about data types. For example:
WorksheetFunction.Sum– ignores text but returns #VALUE! if the range contains an error value.WorksheetFunction.VLookup– returns #N/A if the lookup value is not found, but #VALUE! if the table array contains text where a number is expected.WorksheetFunction.Match– similar issues.
To avoid these, always validate your data before calling the function. For instance, check that a lookup value is numeric before using VLookup.
How to Handle Empty Cells and Null Values
Empty cells can cause “not a number” issues. In VBA, an empty cell’s .Value is Empty. You can test for it with IsEmpty. A cell that contains a formula that returns an empty string (“”) is different – it’s a zero‑length string, which is not numeric.
Here’s a robust pattern:
Dim v As Variant
v = Range("A1").Value
If IsEmpty(v) Then
' treat as 0 or skip
ElseIf IsNumeric(v) Then
' use it as a number
Else
' it's text or an error
End If
If the cell contains an error like #REF!, IsError will catch it. You can use IsError(v) to avoid passing errors to functions.
Using VBA to Fix a Column of Text Numbers in Excel
If you have a column where numbers are stored as text, you can fix it with a quick macro that multiplies each cell by 1 (a common trick) or uses Value assignment. Here’s a complete macro:
Sub FixTextNumbers()
Dim rng As Range
Dim cell As Range
' Set this to your actual range
Set rng = Range("A1:A100")
For Each cell In rng
If IsNumeric(cell.Value) And Not IsEmpty(cell.Value) Then
' Convert text to number by assigning the numeric value
cell.Value = CDbl(cell.Value)
End If
Next cell
End Sub
This works because CDbl converts a numeric string to a double, and assigning it back to the cell changes the cell’s data type from text to number. The macro also skips empty cells.
Frequently Asked Questions
Why does IsNumeric return True for an empty string?
Actually, IsNumeric("") returns False in VBA. However, IsNumeric(Empty) returns True because an empty variant is treated as 0. That can be confusing. Use IsEmpty first to distinguish.
How do I prevent #VALUE! when using WorksheetFunction.Sum?
The Sum function in VBA ignores text, but if the range contains an error value (like #DIV/0!), it will return #VALUE!. Use If IsError(cell.Value) to skip error cells, or use Application.WorksheetFunction.Sum only on validated ranges.
What is the difference between Val and CDbl?Val reads leading numeric characters and ignores the rest, so Val("12abc") returns 12. CDbl requires the entire string to be a valid number, otherwise it raises a type mismatch error. Use Val when you expect extra text, and CDbl when you want strict validation.
Can I use the worksheet function ISNUMBER in VBA?
Yes, you can call WorksheetFunction.IsNumber(cell.Value), but it’s simpler to use VBA’s IsNumeric. Note that IsNumber returns True only for actual numbers, not for numeric strings, while IsNumeric returns True for numeric strings too.
How do I check if a cell contains a number but not a date?
Dates are stored as numbers in Excel. To exclude dates, check the cell’s NumberFormat or use VarType. For example, If VarType(cell.Value) = vbDouble And Not IsDate(cell.Value).
Why does my macro work on some computers but not others?
This often happens because regional settings differ. For example, a decimal comma vs. decimal point. CDbl uses the system’s locale, so a string like “1,5” may be interpreted as 1.5 in some regions and as text in others. Use Val with a period, or replace commas with periods before conversion.
Final Tips for Avoiding “Not a Number” Errors
- Always use
Option Explicitat the top of your module to catch undeclared variables. - Validate data before doing arithmetic:
If IsNumeric(x) Then. - Be aware that
IsNumerictreatsTrueandFalseas numbers (True = -1, False = 0). Filter those out if needed. - When reading from a range, use
.Value(not.Text) to avoid formatting issues, but note that.Textcan be useful for seeing what the user sees. - If you’re working with a large dataset, consider using Excel’s built‑in “Text to Columns” feature (Data > Text to Columns) to convert text numbers in one step, then run a macro for the rest.
By understanding how VBA treats text, empty values, and errors, you can write macros that handle “not a number” gracefully and keep your spreadsheets error‑free.
Related
- Data Entry VA: What It Is, Duties & How to Hire One
- Random 8 Digit Code: How to Generate and Use Them
- GST Invoice Template Excel: Free & How to Use
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- How to Write a For Loop in Excel VBA, With ExamplesTo write a For loop in Excel VBA, open the Visual Basic Editor, insert a module, and place…
- VBA Range Properties: Values, Formatting, and NavigationIn Excel VBA, a Range property either returns information about cells or another range object, or changes a…
- Excel VBA Sleep: Pause a Macro SafelyTo pause an Excel VBA macro on Windows, declare the Windows API function Sleep in a standard VBA…
- VBA Range Sum: Methods, Examples, and Common ErrorsTo sum a range in Excel VBA, use Application.WorksheetFunction.Sum and assign its result to a variable or cell.…
- VBA Macro Copy and Paste: Methods, Examples, and SafetyIn Excel VBA, copy and paste cells with the Range.Copy method: specify the source range and a destination,…
- Excel VBA Exit Function: When and How to Use It SafelyIn Excel VBA, Exit Function stops the current function immediately and returns control to the statement that called…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.