VBA Excel - Does cell have number formatted as text?

Magnet Doctor

New Member
Joined
Apr 16, 2010
Messages
10
I am working on a macro that loops through a bunch of cells in an Excel 2007 worksheet. It would be most convenient if I could determine for each cell what is in the cell, where the choices are 1) a number formatted as text, 2) a number, 3) a blank, 4) text. The flow in the loop goes differently depending on the answer. Identifying numbers and text is easy, but I'm having trouble figuring out how to tell whether the text is actually a formatted number. Can I set up some error handling, then try to convert the text to a number? Is there a better way?
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Does this User Defined Function meet your needs?

<font face=Courier New><SPAN style="color:#00007F">Public</SPAN> <SPAN style="color:#00007F">Function</SPAN> MyType(R <SPAN style="color:#00007F">As</SPAN> Range) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br>  <SPAN style="color:#00007F">If</SPAN> Len(R.Value) = 0 <SPAN style="color:#00007F">Then</SPAN><br>     MyType = "Blank"<br>     <SPAN style="color:#00007F">Exit</SPAN> <SPAN style="color:#00007F">Function</SPAN><br>  <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>  <SPAN style="color:#00007F">If</SPAN> R.NumberFormat = "@" <SPAN style="color:#00007F">Then</SPAN><br>     <SPAN style="color:#00007F">If</SPAN> IsNumeric(R.Value) <SPAN style="color:#00007F">Then</SPAN> MyType = "Number Formatted as Text"<br>     <SPAN style="color:#00007F">If</SPAN> <SPAN style="color:#00007F">Not</SPAN> IsNumeric(R.Value) <SPAN style="color:#00007F">Then</SPAN> MyType = "Text"<br>     <SPAN style="color:#00007F">Exit</SPAN> <SPAN style="color:#00007F">Function</SPAN><br>  <SPAN style="color:#00007F">Else</SPAN><br>    <SPAN style="color:#00007F">If</SPAN> IsNumeric(R.Value) <SPAN style="color:#00007F">Then</SPAN> MyType = "Number"<br>    <SPAN style="color:#00007F">If</SPAN> <SPAN style="color:#00007F">Not</SPAN> IsNumeric(R.Value) <SPAN style="color:#00007F">Then</SPAN> MyType = "Text"<br>  <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Function</SPAN></FONT>
 
Upvote 0
Thanks, again. It turns out that for my purposes I really have no need for the NumberFormat property. The combination of IsNumeric() and IsText() give me the information I really need. IsNumeric() tells me whether or not whatever is in the cell looks like number - either a number or a string that can be converted to a number.
 
Upvote 0

Forum statistics

Threads
1,224,551
Messages
6,179,472
Members
452,915
Latest member
hannnahheileen

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