Testing a single cell range for a number

pglufkin

Board Regular
Joined
Jun 19, 2005
Messages
127
I have a range variable called 'DataVar' which is always a single cell range. DataVar is defined as a range.

I want to test to see if the first character in the cell DataVar represents is a number like thus:

If IsNumeric(Left(DataVar.Text, 1)) = True Then
DataVar.Copy

I am getting the error 'Object variable or With block variable not set' because I think DataVar is defined as a range. However this works if 'Activecell' takes the place of 'DataVar'

I just want to see if the contents of DataVar's first character is a number. Thank you.
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Thank you, I had assigned the range through excel, but I guess you have to assign it through VBA. Thank you.
You said in your original post that you "have a range variable called 'DataVar' which is always a single cell range. DataVar is defined as a range"... how do you declare a range variable as a Range in "Excel" when variables are a VBA entity? Did you mean you created a Defined Name in Excel for that cell? If so, then you would not need a variable in VBA as you can reference the Defined Name directly from your code through the Range object. Assuming that is what you meant, then your If..Then test should look like this...

If IsNumeric(Left(Range("DataVar").Text, 1)) = True Then
Range("DataVar").Copy

I would point out that another way to test if the first character is a digit is like this...

If Range("DataVar").Text Like "#" Then
Range("DataVar").Copy
 
Upvote 0
Thank you, I now see the distinction with defining a name in excel and setting a range in VBA. I like defining the name in Excel, eliminates a step in VBA.

You said in your original post that you "have a range variable called 'DataVar' which is always a single cell range. DataVar is defined as a range"... how do you declare a range variable as a Range in "Excel" when variables are a VBA entity? Did you mean you created a Defined Name in Excel for that cell? If so, then you would not need a variable in VBA as you can reference the Defined Name directly from your code through the Range object. Assuming that is what you meant, then your If..Then test should look like this...

If IsNumeric(Left(Range("DataVar").Text, 1)) = True Then
Range("DataVar").Copy

I would point out that another way to test if the first character is a digit is like this...

If Range("DataVar").Text Like "#" Then
Range("DataVar").Copy
 
Upvote 0

Forum statistics

Threads
1,206,811
Messages
6,074,998
Members
446,112
Latest member
nmz1133

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