Using VBA to Perform Text To Columns, While Preserving Date and Time Formatting

reberryjr

Well-known Member
Joined
Mar 16, 2017
Messages
714
Office Version
  1. 365
Platform
  1. Windows
I need to take several fields with date/time formatting like this, and separate the time from the date. I've been formatting the column as MM/DD/YY HH:MM, which appears fine, but the value in the formula bar doesn't change. So, when I do text to columns, suing SPACE as the delimiter, I lose the 24 hour aspect. I even try to reformat the new Time column as HH:MM, but it doesn't hold.


Thoughts?
[TABLE="class: grid, width: 300"]
<tbody>[TR]
[TD]Incoming Data[/TD]
[TD]What I Want[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]6/13/2018 3:52 PM[/TD]
[TD]06/13/18[/TD]
[TD]15:52[/TD]
[/TR]
</tbody>[/TABLE]
[TABLE="width: 128"]
[/TABLE]
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Nevermind. I was able to piece it together.
Code:
'Performs Text to Columns, separating the Date from the Time, and formats the Date as MM/DD/YY.
With sws.Range("D2:D" & slR)
    .TextToColumns Destination:=sws.Range("D2"), _
    DataType:=xlFixedWidth, _
    FieldInfo:=Array(Array(0, 3), Array(10, 1)), TrailingMinusNumbers:=True
    .NumberFormat = "mm/dd/yy;@"
End With
'Formats the Time as 24 Hour.
With sws.Range("E2:E" & slR)
    .NumberFormat = "hh:mm"
End With
 
Upvote 0

Forum statistics

Threads
1,223,238
Messages
6,170,939
Members
452,368
Latest member
jayp2104

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top