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

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
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,507
Messages
6,114,029
Members
448,543
Latest member
MartinLarkin

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