Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 788
Default Find the Format of a cell

I have a spreadsheet that allows the user to input mostly numeric values but
once in a while will enter a Text value. I run conditional code to do
something if the numeric value falls into a certain range. I need to check
the format of the cell before I apply the condition to make sure it is
numeric.

How do I check the cells format prior to apply the code. Here is what I
tried but kept getting an object required error

For CellNum = 10 To 22
If (["C" & "CellNum"].NumberFormat) = False Then
GoTo SkipCell
Else...

Thanks for all of your help.

-Chris

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,651
Default Find the Format of a cell

On Thu, 16 Jun 2005 15:06:05 -0700, "Chris"
wrote:

I have a spreadsheet that allows the user to input mostly numeric values but
once in a while will enter a Text value. I run conditional code to do
something if the numeric value falls into a certain range. I need to check
the format of the cell before I apply the condition to make sure it is
numeric.

How do I check the cells format prior to apply the code. Here is what I
tried but kept getting an object required error

For CellNum = 10 To 22
If (["C" & "CellNum"].NumberFormat) = False Then
GoTo SkipCell
Else...

Thanks for all of your help.

-Chris



1. Your syntax for your range reference is wrong.
2. Since all cells have a NumberFormat property, your condition test will
never evaluate to True.

---------------------

If your intention is to only allow numeric entries, then use data validation.

If your intention is to only run some test on numeric entries, then test to see
if the entry is a number.

-------------------

See how a cell can have the same property yet still contain either numbers or
text:

Sub foo()
Dim CellNum As Long

For CellNum = 10 To 22 Step 2
With Range("C" & CellNum)
.NumberFormat = "0.00"
.Value = "This is a String"
.Offset(1, 0).NumberFormat = "0.00"
.Offset(1, 0).Value = 275
End With
Next CellNum


For CellNum = 10 To 22
Debug.Print _
Range("C" & CellNum).Address & " " & "Format: " & _
Range("C" & CellNum).NumberFormat & " " & "Cell contents: " & _
Range("C" & CellNum).Text
Next CellNum

End Sub

Gives an output of:

$C$10 Format: 0.00 Cell contents: This is a String
$C$11 Format: 0.00 Cell contents: 275.00
$C$12 Format: 0.00 Cell contents: This is a String
$C$13 Format: 0.00 Cell contents: 275.00
$C$14 Format: 0.00 Cell contents: This is a String
$C$15 Format: 0.00 Cell contents: 275.00
$C$16 Format: 0.00 Cell contents: This is a String
$C$17 Format: 0.00 Cell contents: 275.00
$C$18 Format: 0.00 Cell contents: This is a String
$C$19 Format: 0.00 Cell contents: 275.00
$C$20 Format: 0.00 Cell contents: This is a String
$C$21 Format: 0.00 Cell contents: 275.00
$C$22 Format: 0.00 Cell contents: This is a String

---------------------------
All had numeric formats; but some contain text and some numbers.

What you might consider is

IsNumeric(Range("C" & CellNum).Value)

which will return TRUE or FALSE depending on the cell contents.





--ron
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 788
Default Find the Format of a cell

Excellent, Thanks Ron

-Chris

"Ron Rosenfeld" wrote:

On Thu, 16 Jun 2005 15:06:05 -0700, "Chris"
wrote:

I have a spreadsheet that allows the user to input mostly numeric values but
once in a while will enter a Text value. I run conditional code to do
something if the numeric value falls into a certain range. I need to check
the format of the cell before I apply the condition to make sure it is
numeric.

How do I check the cells format prior to apply the code. Here is what I
tried but kept getting an object required error

For CellNum = 10 To 22
If (["C" & "CellNum"].NumberFormat) = False Then
GoTo SkipCell
Else...

Thanks for all of your help.

-Chris



1. Your syntax for your range reference is wrong.
2. Since all cells have a NumberFormat property, your condition test will
never evaluate to True.

---------------------

If your intention is to only allow numeric entries, then use data validation.

If your intention is to only run some test on numeric entries, then test to see
if the entry is a number.

-------------------

See how a cell can have the same property yet still contain either numbers or
text:

Sub foo()
Dim CellNum As Long

For CellNum = 10 To 22 Step 2
With Range("C" & CellNum)
.NumberFormat = "0.00"
.Value = "This is a String"
.Offset(1, 0).NumberFormat = "0.00"
.Offset(1, 0).Value = 275
End With
Next CellNum


For CellNum = 10 To 22
Debug.Print _
Range("C" & CellNum).Address & " " & "Format: " & _
Range("C" & CellNum).NumberFormat & " " & "Cell contents: " & _
Range("C" & CellNum).Text
Next CellNum

End Sub

Gives an output of:

$C$10 Format: 0.00 Cell contents: This is a String
$C$11 Format: 0.00 Cell contents: 275.00
$C$12 Format: 0.00 Cell contents: This is a String
$C$13 Format: 0.00 Cell contents: 275.00
$C$14 Format: 0.00 Cell contents: This is a String
$C$15 Format: 0.00 Cell contents: 275.00
$C$16 Format: 0.00 Cell contents: This is a String
$C$17 Format: 0.00 Cell contents: 275.00
$C$18 Format: 0.00 Cell contents: This is a String
$C$19 Format: 0.00 Cell contents: 275.00
$C$20 Format: 0.00 Cell contents: This is a String
$C$21 Format: 0.00 Cell contents: 275.00
$C$22 Format: 0.00 Cell contents: This is a String

---------------------------
All had numeric formats; but some contain text and some numbers.

What you might consider is

IsNumeric(Range("C" & CellNum).Value)

which will return TRUE or FALSE depending on the cell contents.





--ron

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,651
Default Find the Format of a cell

On Fri, 17 Jun 2005 04:52:03 -0700, "Chris"
wrote:

Excellent, Thanks Ron

-Chris


You're welcome. Glad to help.

--ron
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to find a cell with a particular date format? Tom Excel Discussion (Misc queries) 3 December 18th 09 06:08 AM
Find/Replace changes cell format ?????? Woland99 Excel Discussion (Misc queries) 2 January 23rd 08 10:07 AM
Find partial cell format icq_giggles Excel Discussion (Misc queries) 7 July 21st 07 12:02 AM
find word in a cell then change cell format [email protected] Excel Programming 2 May 23rd 05 01:21 PM
macro to find date format in a cell and delete that entire row vikram Excel Programming 8 April 30th 04 06:00 PM


All times are GMT +1. The time now is 03:10 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"