VBA & Macro Problems

Keep a 24-Hour Time Format After VBA Text to Columns in Excel

Nimra MalikNimra Malik Dec 2, 2026 868 views

Question details

Keep the time displayed in 24-hour format after splitting a date-time value with VBA Text to Columns.

Keep a 24-Hour Time Format After VBA Text to Columns in Excel
Product
Microsoft Excel
Device & OS
not provided
Scenario
A VBA macro splits space-separated date and time values into separate worksheet columns.
Observed behavior
The split time may display with AM or PM because the destination column uses a 12-hour number format, even though the value is a valid Excel time.
Before you start

Check that the source cells contain date and time text separated by a space, and identify the source date order. Make sure the destination columns are empty or do not contain data you need, because Text to Columns can overwrite them.

Solution 1Recommended

Set the destination time format in the VBA macro

Use this approach to split the date and time and automatically display the resulting time in 24-hour format.

The example reads column U, places the date in column V and the time in column W, and assumes the source date is in day-month-year order. Change the ranges and date format if your data is different.

1
Open the VBA editor

In Excel, open the Developer tab and select Visual Basic. In the VBA editor, select Insert > Module.

2
Add the splitting macro

Paste the following code into the module: Sub SplitDateTime24Hour() Range("U:U").TextToColumns Destination:=Range("V1"), _ DataType:=xlDelimited, ConsecutiveDelimiter:=True, Space:=True, _ FieldInfo:=Array(Array(1, xlDMYFormat), Array(2, xlGeneralFormat)) Columns("W:W").NumberFormat = "hh:mm" End Sub

3
Match the source date order

In the FieldInfo argument, replace xlDMYFormat with xlMDYFormat for month-day-year dates or xlYMDFormat for year-month-day dates. Leave the second field as xlGeneralFormat so Excel can interpret the time.

4
Run the macro

Return to the worksheet containing the data, open Developer > Macros, select SplitDateTime24Hour, and click Run. Check that column W displays times such as 19:09.

Set the destination time format in the VBA macro
Clock time or elapsed time: Use "hh:mm" for a 24-hour clock display. Use "[h]:mm" instead when the values are elapsed durations and hours may exceed 24.
Free Microsoft Office alternative

Work with 24-hour times in WPS Spreadsheets

WPS Office is a free, lightweight alternative with a familiar spreadsheet interface. Open compatible Excel files, format time cells, and move between office tools with ease; it does not fix Excel's VBA behavior.

Apply custom number formats to display spreadsheet times in 24-hour notation.Open and edit Microsoft Excel formats such as XLSX and XLS.Use a familiar interface for everyday spreadsheet tasks.Move existing spreadsheet files into a lightweight office suite.
microsoft office alternative - wps office

Frequently Asked Questions

Why does Excel show AM or PM after Text to Columns?

The destination cells may have a 12-hour number format. Select the time cells and use Format Cells > Number > Custom with hh:mm to display valid time values in 24-hour notation.

What is the difference between hh:mm and [h]:mm?

Use hh:mm for clock times, such as 19:09. Use [h]:mm for elapsed durations when the total hours can exceed 24.

Which VBA date format should I use in FieldInfo?

Match the format to the source text: use xlDMYFormat for day-month-year, xlMDYFormat for month-day-year, or xlYMDFormat for year-month-day.

Why does changing the number format leave the time unchanged?

The cell may contain text instead of a numeric Excel time. Check the split results, then rerun Text to Columns with the time field set to General and apply the hh:mm format.