Change cell formatting in VBA

andrewb90

Well-known Member
Joined
Dec 16, 2009
Messages
1,077
Hello all,

I used a macro recorder to capture what I wanted, but when I run my code, the formatting doesn't seem to change. I'm using an If statment, and the value in the cell changes, so I know the code is working, but the formatting isnt.

Code:
    Range("L23").Select
    Selection.NumberFormat = "[$-en-US]h:mm AM/PM;@"
this one is a time

Code:
    Range("L23").Select
    Selection.NumberFormat = "m/d;@"
this one is a date


any help would be appreciated.
 
Correct, but I need the way it looks to be changed. With my cell formatted as a date, when 9:15 PM is entered all I read is 1/0. And looking at the formula it shows 1/0/1900 9:15 PM. If all my values were time or date, then it would be easy because any value would appear the same.
 
Upvote 0

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
9:15 PM is entered all I read is 1/0
Yes, it is important to understand how Excel stores date and times. They are actually both numbers, and every valid date or time entry has both date and time components.
Basically, Excel stores Dates/Times as the number of days since 1/0/1900. The date portion is a whole number, and the time component is the decimal.
If you enter a date with no time, it just assumes that the time (decimal) part is 0.
If you enter a time with no date, it just assumes that the date (whole number) part is 0.
To see this in action, enter a few dates and times, and then change their format to General. Then you will see them as Excel sees them.

So as long as your entries have just a date component, or just a time component (and never both), something like this should work:
Code:
    If Sheets("PDFPage").Range("L23") >= 1 Then
        Sheets("PDFPage").Range("L23").NumberFormat = "m/d;@"
    Else
        Sheets("PDFPage").Range("L23").NumberFormat = "[$-en-US]h:mm AM/PM;@"
    End If
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,194
Members
453,021
Latest member
pingpong7117

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