Changing Time Format with a macro

ceefor

Board Regular
Joined
Jan 17, 2009
Messages
69
Hello everyone,

I am well aware of how to change the format of a time in a cell, however Excel doesn't recognize the following format: "05:23pm" as an actual time format, therefore changing the format is futile.

I imagine that I need to add a space between the ":23" and "PM" for it to recognize and allow me to manipulate which time format I want.

I thought of recording a macro that would replace "xpm" with "x PM", however I am not sure what I would use for the wildcard.

Any help here is appreciated as always.

Thank you,
ceefor
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
If you do a find/replace and replace "pm" with " pm" (without the quotes) you'll be fine without a wildcard.

Here's a simple vba macro made with the recorder that will do the trick for you:

Code:
Sub AMPMChange()
    Cells.Replace What:="pm", Replacement:=" pm", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
    Cells.Replace What:="am", Replacement:=" am", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,513
Messages
6,179,214
Members
452,895
Latest member
BILLING GUY

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