Convert Whole Numbers to Times in Excel with VBA
Question details
Convert whole-number entries such as 534 into Excel time values such as 5:34.

- 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.
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.
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.
In desktop Excel, right-click the target worksheet tab and select View Code.
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
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.
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 existing numbers with an Excel formula
Use a separate column when you do not need automatic conversion in the original entry cells.
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. Open your spreadsheet: Open WPS Spreadsheets and use Open to select your workbook.
- 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. Fill the column: Drag the fill handle from B2 down to convert the other entries.
- 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.

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.




