Getting cell format in VBA.

HighAndWilder

Active Member
Joined
Nov 4, 2006
Messages
392
Office Version
  1. 365
Platform
  1. Windows
Hi All.

I have a cell with a number format of DD/MM/YYYY HH:MM.

I want to be able to ascertain this in VBA.

Using the following returns m/d/yyyy h:mm.

strFormat = range("A1").NumberFormat

I need the exact format to format text in a Userform listbox.

How can this be achieved?

Thanks
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Are you sure that you are checking the correct cell?
And, are you sure that you are on the right sheet when you run that code?

If I apply that exact format to cell A1, and then run this VBA code:
VBA Code:
Sub Test()
    MsgBox Range("A1").NumberFormat
End Sub
it returns the following for me: dd/mm/yyyy hh:mm
which appears to be exactly what you are looking for.
 
Upvote 0
Correct sheet and correct cell.

In the Format Cell Dialog Box the cell format is dd/mm/yyyy hh:mm but when I try to access it in VBA I get m/d/yyyy h:mm.

If I change the format in VBA to dd/mm/yyyy hh:mm I then get the correct format when I use the following code.

strFormat = range("A1").NumberFormat
 
Upvote 0
What does it return if your try this?
VBA Code:
Sub Test2()
    Range("A1").NumberFormat = "dd/mm/yyyy hh:mm"
    MsgBox Range("A1").NumberFormat
End Sub

If that still does not return what you expect, try this version:
VBA Code:
Sub Test3()
    Application.EnableEvents = False
    Range("A1").NumberFormat = "dd/mm/yyyy hh:mm"
    MsgBox Range("A1").NumberFormat
    Application.EnableEvents = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,875
Messages
6,122,044
Members
449,063
Latest member
ak94

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