Convert Four-Digit Numbers to 24-Hour Times with Excel VBA
Question details
Convert four-digit hour-and-minute entries into real Excel time values in the same cells.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- A user enters values such as 0430, 0030, and 1330 and wants to convert them without an extra column.
- Observed behavior
- The converted values should display in 24-hour format, such as 04:30, rather than with AM/PM formatting.
Select cells containing unconverted hour-and-minute entries, and make sure macros are available and enabled for a trusted workbook. Save the workbook as an Excel Macro-Enabled Workbook (.xlsm) if you want to retain the macro.
Convert selected cells in place with an Excel VBA macro
Use this macro to replace valid four-digit-style entries with real time values and display them as hours and minutes.
The macro treats numeric 30 as 0030 and 430 as 0430, so leading zeros do not need to be stored. It skips formulas, errors, nonnumeric entries, and values with invalid hours or minutes.
In Excel, open the workbook and select Developer > Visual Basic, or press Alt+F11.
Choose Insert > Module and paste this code: Sub ConvertSelectedTimes() Dim c As Range Dim t As Double Dim h As Long Dim m As Long For Each c In Selection.Cells If Not c.HasFormula Then If Not IsError(c.Value2) Then If IsNumeric(c.Value2) And Not IsEmpty(c.Value2) Then t = CDbl(c.Value2) If t >= 0 And t <= 2359 And t = Fix(t) Then h = Int(t / 100) m = t - h * 100 If h < 24 And m < 60 Then c.NumberFormat = "hh:mm" c.Value = TimeSerial(h, m, 0) End If End If End If End If End If Next c End Sub
Return to the worksheet, select the cells to convert, then choose Developer > Macros. Select ConvertSelectedTimes and click Run.
Confirm that entries such as 0030 and 1330 display as 00:30 and 13:30. If necessary, select the cells and use Format Cells > Custom with the type hh:mm.

Convert selected time entries in WPS Spreadsheets
If your WPS Office installation supports VBA macros, use the same in-place conversion macro in WPS Spreadsheets and format the results as 24-hour times. VBA availability varies by version and edition.
- 1. Open the workbook: Open the Excel workbook in WPS Spreadsheets and check whether the Developer tab and VBA tools are available.
- 2. Add the macro: If VBA is supported, choose Developer > Visual Basic, insert a module, and paste the ConvertSelectedTimes code from the solution above.
- 3. Convert the cells: Return to the sheet, select the unconverted entries, and run ConvertSelectedTimes from the macros list.
- 4. Save the result: Check the hh:mm display and use File > Save As to save in a macro-enabled format if you need to retain the macro.

Frequently Asked Questions
Why does the converted time show AM or PM?
The cell may still use a 12-hour number format. Select the cells, open Format Cells > Custom, and enter hh:mm to display 24-hour time.
Can the macro convert 0030 if Excel stores it as 30?
Yes. The macro divides the numeric value into hours and minutes, so 30 becomes 00:30 and 430 becomes 04:30.
What happens to entries such as 1260 or 2400?
The macro skips them because minutes must be below 60 and hours must be below 24. Correct those entries in the worksheet before running the macro again on the affected cells.
Can new entries be converted automatically?
Yes. A Worksheet_Change event can run conversion code when cells are edited, but it should be restricted to the intended range and prevent the event from triggering itself.




