VBA Code for Time Format as HH:MM AM/PM

OilEconomist

Active Member
Joined
Dec 26, 2016
Messages
421
Office Version
  1. 2019
Platform
  1. Windows
Thanks in advance for your assistance. I would like to format a cell in VBA with the time format. I wrote the code, but that doesn't work. Just something to start.

Some point outs.
Hours show up with two digits, but I do not want military time.
If I do not enter AM or PM, it defaults to AM automatically.

Example 1:
Entry: 4 AM
Result: 04:00 AM EST

Example 2:
Entry: 4 PM
Result: 04:00 PM EST

Example 3:
Entry: 4
Result: 04:00 AM EST

VBA Code:
Sub TimeFormatter()

For i = 1 to 100
Range("A" & i).NumberFormat = "HH:MM AM/PM EST"

End Sub
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Those results are desired results, not what you get?
If you only enter a single digit number (e.g. 4), you will get the 4th day of January 1900 at 12:00 AM if the cell is formatted as a number (or so I think).
If you want 4 and not 04, then alter the HH to H
I don't believe EST is a valid parameter for date/time formats but happy to be proved wrong. Do you have a reference that states otherwise?
If your cell is formatted as text I don't believe that code will have any effect.
Possibly you could use Time function if you can provide hour, minute and second values somehow, but you'd have to pass 13 to it to get 1:00 PM.
 
Upvote 0
Those results are desired results, not what you get?
If you only enter a single digit number (e.g. 4), you will get the 4th day of January 1900 at 12:00 AM if the cell is formatted as a number (or so I think).
If you want 4 and not 04, then alter the HH to H
I don't believe EST is a valid parameter for date/time formats but happy to be proved wrong. Do you have a reference that states otherwise?
If your cell is formatted as text I don't believe that code will have any effect.
Possibly you could use Time function if you can provide hour, minute and second values somehow, but you'd have to pass 13 to it to get 1:00 PM.
Those are desired results.

I understand right now if I enter a single digit, I may get will get the 4th day of January 1900 at 12:00 AM.

No I do not have a reference that states EST is a valid parameter for date/time.

Passing 13 to get 01:00 PM EST is fine.

I don't know if there is a VBA code that is able to do what I would like, but that's why I posted here.
 
Upvote 0
The attached shows function examples in 2 cells; the cells as custom formatted.

You can use VBA.
 
Upvote 0
reposted
The attached shows function examples in 2 cells; the cells are custom formatted.
You can use Excel's macros recorder as you enter the information and then convert the relevant parts to your VBA.


You can use VBA.
Date and Time_21.xlsm
ABC
1
204:00 AM ESTcustom formatted hh:mm AM/PM "EST"
305:00 PM EST
6c
Cell Formulas
RangeFormula
A2A2=TIME(4,0,0)
 
Upvote 0
reposted
The attached shows function examples in 2 cells; the cells are custom formatted.
You can use Excel's macros recorder as you enter the information and then convert the relevant parts to your VBA.


You can use VBA.
Date and Time_21.xlsm
ABC
1
204:00 AM ESTcustom formatted hh:mm AM/PM "EST"
305:00 PM EST
6c
Cell Formulas
RangeFormula
A2A2=TIME(4,0,0)
Thanks @Dave Patton , do I need to download XL2BB to obtain the code?
 
Upvote 0
Thanks @Dave Patton , do I need to download XL2BB to obtain the code?

I did not include any code.
I showed an example entering time with an Excel function time and entering the time in the cell 17:00:00'

I then custom formatted the 2 cells. You can paste my post into a clean sheet.

Click on the icon below the f(x) in the heading of the post example and then move to your sheet and paste.
 
Upvote 0
Thanks @Dave Patton , do I need to download XL2BB to obtain the code?

I did not include any code.
I showed an example entering time with an Excel function time and entering the time in the cell 17:00:00'

I then custom formatted the 2 cells. You can paste my post into a clean sheet.

Click on the icon below the f(x) in the heading of the post example and then move to your sheet and paste.
Thanks @Dave Patton, I came up with the following. I just need to see if I can trim it down to one line.


Sub Test
VBA Code:
Macro2()

For i = 6 To 11
    Range("D" & i) = "=TIME(B" & i & ", 0, 0)"
    Range("D" & i).NumberFormat = "hh:mm AM/PM ""EST"""
    Range("D" & i).Formula = Range("D" & i).Value
Next i
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,985
Messages
6,122,605
Members
449,089
Latest member
Motoracer88

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