VBA Test if cell content is a number

evert

Board Regular
Joined
Dec 11, 2007
Messages
64
Hi,

How can I test or check, in VBA code, whether the content of a given cell is a number?

Thanks.
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Take a look at the IsNumeric help in the VBA editor

KR


Dave
 
Upvote 0
Maybe

Code:
Sub TestForNumber()
Dim Rng As Range
Set Rng = Range("A1")
    If IsNumeric(Rng) Then
        MsgBox Rng.Address(False, False) & " is a Number"
    Else
        MsgBox Rng.Address(False, False) & " is not a Number"
    End If
End Sub

VBA Noob
 
Upvote 0
IsNumeric will return TRUE for empty cells, because empty cells can be evaluated as the number 0:

Code:
Sub test()
Dim c As Range, msg As String
msg = " is not a number"
Set c = Range("A1")
If IsNumeric(c) And c <> "" Then msg = " is a number"
MsgBox c.Address(0, 0) & msg
End Sub
 
Upvote 0

Forum statistics

Threads
1,213,517
Messages
6,114,085
Members
448,548
Latest member
harryls

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top