Insert a space before AM or PM in a text column

Crocdundee

Board Regular
Joined
May 10, 2010
Messages
174
Office Version
  1. 2013
Platform
  1. Windows
Hi I have a column of text which really is a date and time combined, however I can not sort correctly with this format.
let me show you the data
11:01AM 23 Aug 2021
11:22AM 23 Aug 2021
11:43AM 23 Aug 2021
12:04PM 23 Aug 2021
12:53PM 23 Aug 2021
1:33PM 23 Aug 2021
2:12PM 23 Aug 2021
2:48PM 23 Aug 2021
1:00PM 23 Aug 2021
1:30PM 23 Aug 2021
2:00PM 23 Aug 2021
2:30PM 23 Aug 2021
3:00PM 23 Aug 2021
Data is in column B2:B
In Reality I need a macro to put a space before the AM or PM
Regards Graham
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Hi,
try this..
VBA Code:
Sub replace()
    With Range("B2:B" & Cells(Rows.Count, 2).End(3).Row)
        .replace "AM", " AM"
        .replace "PM", " PM"
    End With
End Sub
 
Upvote 0
Hi,
try this..
VBA Code:
Sub replace()
    With Range("B2:B" & Cells(Rows.Count, 2).End(3).Row)
        .replace "AM", " AM"
        .replace "PM", " PM"
    End With
End Sub
While the above code will work as written, I would suggest you use a different macro name as Replace is a built-in function in VBA.
 
Upvote 0
Thank you All for answering my Vba question, I always try myself to solve my problems, but this one escaped me
I appreciate all the help that is given here
Regards
Graham
 
Upvote 0

Forum statistics

Threads
1,215,045
Messages
6,122,830
Members
449,096
Latest member
Erald

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