VBA & Macro Problems

Convert Whole Numbers to Times in Excel with VBA

Huda QurayshiHuda Qurayshi Nov 18, 2026 870 views

Question details

Convert whole-number entries such as 534 into Excel time values such as 5:34.

Convert Whole Numbers to Times in Excel with VBA
Product
Microsoft Excel
Device & OS
not provided
Scenario
A user wants digits entered in a worksheet range to be interpreted as hours and minutes.
Observed behavior
Applying a time format alone does not convert 534 into 5:34 because Excel treats the number as a date-time serial value.
Before you start

Choose the worksheet and entry range to convert, and use desktop Excel to edit and run VBA macros. The macro below interprets the last two digits as minutes and accepts hours from 0 to 23.

Solution 1Recommended

Convert entries automatically with a Worksheet_Change macro

Use this method to convert valid whole numbers in the same cells as they are entered.

The example monitors B2:B100 on one worksheet. It converts 534 to 5:34 AM and 1218 to 12:18 PM; invalid entries such as 1260 remain unchanged.

1
Open the worksheet code

In desktop Excel, right-click the target worksheet tab and select View Code.

2
Paste the event macro

Paste this code into the worksheet's code window, replacing B2:B100 with your entry range if needed: Private Sub Worksheet_Change(ByVal Target As Range) Dim affected As Range, c As Range Dim v As Variant, n As Long, h As Long, m As Long Set affected = Intersect(Target, Me.Range("B2:B100")) If affected Is Nothing Then Exit Sub On Error GoTo CleanUp Application.EnableEvents = False For Each c In affected.Cells If Not c.HasFormula Then v = c.Value2 If Not IsError(v) Then If IsNumeric(v) Then If v >= 0 And v <= 2359 And v = Int(v) Then n = CLng(v) h = n \ 100 m = n Mod 100 If h < 24 And m < 60 Then c.Value = TimeSerial(h, m, 0) c.NumberFormat = "h:mm AM/PM" End If End If End If End If End If Next c CleanUp: Application.EnableEvents = True End Sub

3
Save and enable the macro

Select File > Save As and choose Excel Macro-Enabled Workbook (*.xlsm). Reopen the workbook if needed and enable macros only if you trust the file.

4
Test an entry

Enter 534 in a cell within B2:B100 and press Enter. It should display as 5:34 AM; enter 1218 to check the PM display.

Convert entries automatically with a Worksheet_Change macro
Existing values are not converted automatically: The event runs when cells in the monitored range are changed, not when formulas recalculate. For a 24-hour display, change the number format in the code to h:mm.
Convert numbers in WPS Spreadsheets

Convert whole numbers to times in WPS Spreadsheets

Use a formula in WPS Spreadsheets to turn entries such as 534 into time values in a separate column, then format or paste the results as values.

  1. 1. Open your spreadsheet: Open WPS Spreadsheets and use Open to select your workbook.
  2. 2. Enter the formula: With the source number in A2, enter =IF(AND(ISNUMBER(A2),A2=INT(A2),A2>=0,A2<=2359,MOD(A2,100)<60),TIME(INT(A2/100),MOD(A2,100),0),"Invalid") in B2.
  3. 3. Fill the column: Drag the fill handle from B2 down to convert the other entries.
  4. 4. Apply a time format: Select the results, open Format Cells, and choose a time format such as h:mm AM/PM. To keep fixed results, copy them and use Paste Special > Values.
Convert hour-and-minute digits with a worksheet formulaOpen and edit Microsoft Excel formats such as XLSXUse familiar spreadsheet formatting and fill toolsWork with a free, easy-to-use office suite
QA img-9

Frequently Asked Questions

Why doesn't changing the cell format turn 534 into 5:34?

Excel stores dates and times as serial numbers. Formatting changes how the existing value appears; it does not split 534 into hours and minutes.

Where should I paste the Worksheet_Change code?

In desktop Excel, right-click the target sheet tab and select View Code. Paste the event procedure into that worksheet's code window, not a standard module.

Can I display the converted times in 24-hour format?

Yes. In the macro, change the NumberFormat value from h:mm AM/PM to h:mm. For formula results, select the cells and choose a 24-hour time format in Format Cells.

What happens if I enter 1260 or 2400?

The macro leaves those entries unchanged because 60 is not a valid minute and 24 is outside the accepted hour range. The formula returns Invalid for either entry.