Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Excel / General Excel Questions / May 2008

Tip: Looking for answers? Try searching our database.

Get cell's number format category in VBA?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
dyowee - 13 May 2008 10:03 GMT
Good day!
Is this possible to do programmatically?

Thanks.
Gary''s Student - 13 May 2008 10:13 GMT
Sub whatformat()
MsgBox (ActiveCell.NumberFormat)
End Sub

Signature

Gary''s Student - gsnu200785

dyowee - 13 May 2008 10:16 GMT
This returns the actual number format, not the category, like 'General',
'Percentage', 'Currency', etc...

> Sub whatformat()
> MsgBox (ActiveCell.NumberFormat)
> End Sub
Gary''s Student - 13 May 2008 10:41 GMT
I don't think we can get it directly (because catagory is not a property of
the range object).  However code can be written to figure it out.

For example, if the format has a % in it it must be Percent; if the format
has a $ in it it must be Currency; etc.

Need to determine some key characters and then do some kind of lookup.

Good Luck
Signature

Gary''s Student - gsnu200785

> This returns the actual number format, not the category, like 'General',
> 'Percentage', 'Currency', etc...
>
> > Sub whatformat()
> > MsgBox (ActiveCell.NumberFormat)
> > End Sub
Dave Peterson - 13 May 2008 12:42 GMT
Excel has its own
=cell("format",a1)
that will return a category (kind of).

Look at excel's help for =cell() and you'll find a list of those "Categories".

Then you can evaluate that formula and look at the resulting string (first
character or whole string???) to see what category you want.

This evaluates that formula:

   Dim res As String
   With Worksheets("sheet1")
       res = .Evaluate("cell(""Format""," & .Range("a1").Address & ")")
   End With    
   MsgBox res

This is from xl2003's (USA version) help:

If the Microsoft Excel format is      CELL returns
General                               "G"
0                                     "F0"
#,##0                                 ",0"
0.00                                  "F2"
#,##0.00                              ",2"
$#,##0_);($#,##0)                     "C0"
$#,##0_);[Red]($#,##0)                "C0-"
$#,##0.00_);($#,##0.00)               "C2"
$#,##0.00_);[Red]($#,##0.00)          "C2-"
0%                                    "P0"
0.00%                                 "P2"
0.00E+00                              "S2"
# ?/? or # ??/??                      "G"
m/d/yy or m/d/yy h:mm or mm/dd/yy     "D4"
d-mmm-yy or dd-mmm-yy                 "D1"
d-mmm or dd-mmm                       "D2"
mmm-yy                                "D3"
mm/dd                                 "D5"
h:mm AM/PM                            "D7"
h:mm:ss AM/PM                         "D6"
h:mm                                  "D9"
h:mm:ss                               "D8"

But the "G" used for both General and fraction seems pretty disconcerting!

> Good day!
> Is this possible to do programmatically?
>
> Thanks.

Signature

Dave Peterson

dyowee - 14 May 2008 04:36 GMT
Why does "12:00:00 AM" returns a "G"? =(

> Excel has its own
> =cell("format",a1)
[quoted text clipped - 45 lines]
> >
> > Thanks.
Niek Otten - 14 May 2008 09:23 GMT
Maybe because there is text, not a "real" Excel date and time
You can easily check with the ISTEXT() function

Signature

Kind regards,

Niek Otten
Microsoft MVP - Excel

| Why does "12:00:00 AM" returns a "G"? =(
|
[quoted text clipped - 47 lines]
| > >
| > > Thanks.
Dave Peterson - 14 May 2008 12:21 GMT
Just to add to Niek's response...

I entered 12:00:00 AM (as a real time) and got D9.

> Why does "12:00:00 AM" returns a "G"? =(
>
[quoted text clipped - 51 lines]
> >
> > Dave Peterson

Signature

Dave Peterson


Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.