VBA & Macro Problems

Convert Compact AM/PM Entries to Excel Time with VBA

Adam DavisAdam Davis Dec 2, 2026 868 views

Question details

Convert compact entries such as 230p and 8a into Excel time values using VBA.

Convert Compact AM/PM Entries to Excel Time with VBA
Product
Microsoft Excel
Device & OS
not provided
Scenario
A user enters abbreviated AM or PM times in worksheet cells and wants them converted automatically.
Observed behavior
Entries such as 230p and 8a are not consistently interpreted as Excel time values.
Before you start

Open the workbook in a desktop version of Excel that supports VBA and identify the cells where you want the conversion to apply. Save the workbook as an Excel Macro-Enabled Workbook (.xlsm) and enable macros if your organization's security settings permit them.

Solution 1Recommended

Convert entries with a Worksheet_Change macro

Use this worksheet event to convert compact AM/PM entries in a specified range as they are entered or pasted.

The example accepts one to four digits followed by a or p, such as 8a, 230p, or 1230p. Values up to 12 are treated as whole hours; larger values are interpreted as hours and minutes. Invalid entries are left unchanged.

1
Open the worksheet code

In Excel, right-click the target worksheet tab and select View Code. If that command is unavailable, open the VBA editor from Developer > Visual Basic and select the worksheet in the project pane.

2
Add the event macro

Paste the following code into that worksheet's code window. If it already contains a Worksheet_Change procedure, combine the logic with the existing procedure rather than adding a second one. Private Sub Worksheet_Change(ByVal Target As Range) Dim affected As Range, cell As Range Dim s As String, digits As String, suffix As String Dim n As Long, h As Long, m As Long Set affected = Intersect(Target, Me.Range("A1:A100")) If affected Is Nothing Then Exit Sub On Error GoTo CleanUp Application.EnableEvents = False For Each cell In affected.Cells If Not IsError(cell.Value2) And Not cell.HasFormula Then s = LCase$(Trim$(CStr(cell.Value2))) If Len(s) >= 2 And Len(s) <= 5 Then suffix = Right$(s, 1) digits = Left$(s, Len(s) - 1) If (suffix = "a" Or suffix = "p") And Not (digits Like "*[!0-9]*") Then n = CLng(digits) If n <= 12 Then h = n m = 0 Else h = n \ 100 m = n Mod 100 End If If h >= 1 And h <= 12 And m <= 59 Then h = h Mod 12 If suffix = "p" Then h = h + 12 cell.Value = TimeSerial(h, m, 0) cell.NumberFormat = "h:mm AM/PM" End If End If End If End If Next cell CleanUp: Application.EnableEvents = True End Sub

3
Set the target range

In the code, replace A1:A100 in Me.Range("A1:A100") with the cells where you want compact time entries converted.

4
Save and test

Save the workbook as .xlsm, return to the worksheet, and enter 230p or 8a in the target range. The cells should display 2:30 PM and 8:00 AM.

Convert entries with a Worksheet_Change macro
Existing entries: The event runs when cells change; it does not automatically convert entries already in the range. Edit and re-enter an existing value to trigger the conversion.
Free Microsoft Office alternative

Work with time values in WPS Spreadsheets

WPS Office is a free, lightweight alternative for working with spreadsheets in a familiar interface. Open compatible Excel files to continue everyday spreadsheet work; availability of VBA macros depends on your WPS edition and platform.

Enter and format time values in WPS SpreadsheetsOpen and work with common Microsoft Excel formats, including .xlsx and .xlsUse a familiar spreadsheet interface for an easier transition from ExcelWork with a lightweight office suite available for free
microsoft office alternative - wps office

Frequently Asked Questions

Where should I paste the Worksheet_Change code?

Right-click the worksheet tab in Excel and select View Code, then paste the procedure into that worksheet's code window. It will respond to changes on that sheet.

How do I apply the macro to a different range?

Change Me.Range("A1:A100") in the code to the range you want, such as Me.Range("B2:B200"). Only changes within that range will be processed.

Why does the macro disable events?

Writing the converted time back to a cell would otherwise trigger Worksheet_Change again. Application.EnableEvents is set to False during conversion and restored in the cleanup section.

How are noon and midnight handled?

The macro converts 12p to 12:00 PM and 12a to 12:00 AM. It uses TimeSerial to create an Excel time value and applies the h:mm AM/PM number format.

Why didn't an existing entry convert?

Worksheet_Change runs when a cell changes, not when the code is added. Re-enter the value in a cell within the target range, and check that macros are enabled and the workbook is saved as .xlsm.