determine cell format

kmiles

Board Regular
Joined
Apr 1, 2002
Messages
113
using the worksheet_selectchange event, I need to determine if the target cell is formatted in mm/dd/yyyy format. I know I should be able to use some variation of the cell("format") function along with application.worksheetfunction but I can't seem to get this to work.

I know the value returned by the cell function is D4 for the above mentioned format.

All help is greatly appreciated !
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Hello, kmiles,
try this

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.NumberFormat = "mm/dd/yyyy" Then MsgBox "yes"
End Sub

kind regards,
Erik
 
Upvote 0
Sub cellFormat()

myFmat = Selection.NumberFormat

myTyp = myTyp & vbLf & myFmat

MsgBox myTyp

End Sub



To test for a defined type of format:

Sub cellFormatTest()
If Selection.NumberFormat <> "m/d/yyyy" Then MsgBox "Not: m/d/yyyy"
End Sub
 
Upvote 0
Those won't work because the cell is formatted as a date, not a number.

The formula below works in Excel, I just need to make it work in VB.

assuming 3/15/2005 is in A1, formatted as date mm/dd/yyyy

then

in b1
=cell("format",a1) will return the value D4, which I can then easily test for.
 
Upvote 0
Those won't work because the cell is formatted as a date, not a number.
the format has nothing to do with the syntax .NumberFormat
what do you think this code is doing ?
ActiveCell.NumberFormat = "@"
it is formatting as a string ! (not a number)

what do you get using this code ?
Code:
Range("A1") = ActiveCell.NumberFormat
The contents of A1 is what you will have to paste in the code (between quotes)

just using some tricks can solve your problems :) or still no luck ? ...

best regards,
Erik
 
Upvote 0
Thanks for your help Erik.

The code below works exactly as I needed:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

cellFmt = Target.NumberFormat
If Left(cellFmt, 8) = "m/d/yyyy" Then
a = MsgBox("Cell " & Target.Address & " is formatted as m/d/yyyy", vbOKOnly)
End If

End Sub
 
Upvote 0

Forum statistics

Threads
1,207,281
Messages
6,077,511
Members
446,287
Latest member
tjverdugo85

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