VBA & Macro Problems

Convert 5p to 5:00 PM Automatically in Excel

Camila MilosovichCamila Milosovich Dec 2, 2026 868 views

Question details

Automatically convert shorthand entries such as 5p and 3a into Excel time values.

Convert Entries Like 5p to Time Automatically in Excel
Product
Microsoft Excel
Device & OS
not provided
Scenario
A user enters an hour followed by a or p and wants the same cell to display a corresponding AM or PM time.
Observed behavior
Excel treats entries such as 5p as text, and applying a time format alone does not convert them.
Before you start

Open the workbook in a desktop version of Excel that supports VBA, and decide which cells should accept shorthand entries. Save the workbook as an Excel Macro-Enabled Workbook (.xlsm) so the event code can be retained.

Solution 1Recommended

Use a Worksheet Change macro to convert shorthand times

Use this method to replace entries such as 5p with real time values in the same cells as they are entered.

The macro below monitors A1:AZ100 on one worksheet. It accepts whole hours from 1 to 12 followed by a or p, ignoring case and surrounding spaces. Other entries are left unchanged.

1
Open the worksheet code

Right-click the worksheet tab where you enter times and select View Code.

2
Add the event macro

Paste this code into the worksheet's code window. If a Worksheet_Change procedure already exists there, integrate the logic into that procedure rather than adding a second one: Private Sub Worksheet_Change(ByVal Target As Range) Dim affected As Range Dim c As Range Dim s As String Dim h As Long Set affected = Intersect(Target, Me.Range("A1:AZ100")) If affected Is Nothing Then Exit Sub On Error GoTo CleanUp Application.EnableEvents = False For Each c In affected.Cells If Not c.HasFormula And VarType(c.Value2) = vbString Then s = LCase$(Trim$(c.Value2)) If s Like "#[ap]" Or s Like "##[ap]" Then h = CLng(Left$(s, Len(s) - 1)) If h >= 1 And h <= 12 Then c.Value = TimeSerial((h Mod 12) + IIf(Right$(s, 1) = "p", 12, 0), 0, 0) c.NumberFormat = "h:mm AM/PM" End If End If End If Next c CleanUp: Application.EnableEvents = True End Sub

3
Choose the monitored range

In the code, replace A1:AZ100 with the range where you want shorthand conversion. Leave it as written if that range suits your worksheet.

4
Save and test

Save the file as .xlsm, enable macros if prompted and you trust the workbook, then enter 5p in a monitored cell. It should display 5:00 PM; 3a should display 3:00 AM.

Use a Worksheet Change macro to convert shorthand times
Formatting alone does not convert text: The macro converts the entry to a time value and applies the custom format h:mm AM/PM. Its cleanup section re-enables events if an error occurs.
Convert shorthand times in a separate column

Convert shorthand time entries in WPS Spreadsheets

Use a formula in WPS Spreadsheets to turn entries such as 5p into time values in a separate column. This approach does not replace the original entry in the same cell.

  1. 1. Enter shorthand times: Open the workbook in WPS Spreadsheets and enter times such as 5p or 3a in column A. Use whole hours from 1 to 12 followed by a or p.
  2. 2. Add the conversion formula: In B1, enter =IFERROR(TIME(MOD(VALUE(LEFT(TRIM(A1),LEN(TRIM(A1))-1)),12)+IF(LOWER(RIGHT(TRIM(A1),1))="p",12,0),0,0),""). This formula assumes entries follow the stated shorthand pattern.
  3. 3. Format the results: Select column B, open Format Cells, choose Custom, and enter h:mm AM/PM.
  4. 4. Apply the formula to other rows: Drag the fill handle from B1 down alongside the entries in column A. The converted times appear in column B.
Convert AM and PM shorthand without a macroOpen and save Microsoft Excel formats such as XLSXUse a familiar spreadsheet interfaceFill the formula down for multiple entries
microsoft office alternative - wps office

Frequently Asked Questions

Why doesn't the time format turn 5p into 5:00 PM?

The Custom format h:mm AM/PM changes how an existing time value appears; it does not convert text. Use the worksheet macro or a conversion formula first.

How do I make the macro work only in one column?

Open the worksheet's View Code window and change Me.Range("A1:AZ100") to a range such as Me.Range("A1:A100"). Entries outside that range will not be converted.

What happens if I enter 12a or 12p?

The macro converts 12a to 12:00 AM and 12p to 12:00 PM. It accepts whole hours from 1 through 12 followed by a or p.

Why did the macro stop working after I reopened the file?

Check that the workbook was saved as .xlsm and that macros are enabled for the trusted file. Also confirm that the code is in the intended worksheet's View Code window.