VBA Change text column to date format

davidmg1982

Board Regular
Joined
Oct 12, 2015
Messages
64
Im having troubles to convert a text column into dates, but so far my script is just giving me back some crazy numbers only. Thanks ahead for your guidance.

This is how my data looks like:

01312020
02292020
03312020
04302020
05312020

This is what i want in return, along with the date format in the column

01/31/2020
02/29/2020
03/31/2020
04/30/2020
05/31/2020

Sub FormatDate()
With ActiveSheet.UsedRange.Columns("DO").Cells
.TextToColumns Destination:=.Cells(1), DataType:=xlFixedWidth, FieldInfo:=Array(0, xlYMDFormat)
.NumberFormat = "mm/dd/yyyy"
End Sub
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Here are two solutions for you.
1. VBA
VBA Code:
Option Explicit

Sub Dates()
    Dim i As Long, lr As Long
    lr = Range("A" & Rows.Count).End(xlUp).Row
    For i = 1 To lr
        Range("A" & i) = Left(Range("A" & i), 2) & "/" & Mid(Range("A" & i), 3, 2) & "/" & Right(Range("A" & i), 4)
    Next i

End Sub

2. Power Query--Mcode follows
Rich (BB code):
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Split Column by Position" = Table.SplitColumn(Source, "Column1", Splitter.SplitTextByPositions({0, 4}, true), {"Column1.1", "Column1.2"}),
    #"Changed Type" = Table.TransformColumnTypes(#"Split Column by Position",{{"Column1.1", Int64.Type}, {"Column1.2", Int64.Type}}),
    #"Split Column by Position1" = Table.SplitColumn(Table.TransformColumnTypes(#"Changed Type", {{"Column1.1", type text}}, "en-US"), "Column1.1", Splitter.SplitTextByPositions({0, 2}, true), {"Column1.1.1", "Column1.1.2"}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Position1",{{"Column1.1.1", Int64.Type}, {"Column1.1.2", Int64.Type}}),
    #"Merged Columns" = Table.CombineColumns(Table.TransformColumnTypes(#"Changed Type1", {{"Column1.1.1", type text}, {"Column1.1.2", type text}, {"Column1.2", type text}}, "en-US"),{"Column1.1.1", "Column1.1.2", "Column1.2"},Combiner.CombineTextByDelimiter("/", QuoteStyle.None),"Merged"),
    #"Changed Type2" = Table.TransformColumnTypes(#"Merged Columns",{{"Merged", type date}})
in
    #"Changed Type2"

Book1
ABC
11/31/2020Merged
22/29/20201/31/2020
33/31/20202/29/2020
44/30/20203/31/2020
55/31/20204/30/2020
65/31/2020
7
Sheet1
 
Upvote 0
Another VBA without loop ...

VBA Code:
Sub test()
  With ActiveSheet.UsedRange.Columns("DO")
    .Value = Evaluate(Replace("if(@="""","""",left(@,2)&""/""&mid(@,3,2)&""/""&right(@,4))", "@", .Address))
    .NumberFormat = "mm/dd/yyyy"
  End With
End Sub
 
Upvote 0
Here another macro for you to consider:

VBA Code:
Sub ChangeDate()
  With Range("DO1", Range("DO" & Rows.Count).End(3))
    .Value = Evaluate("=IF({1},REPLACE(REPLACE(" & .Address & ",5,0,""/""),3,0,""/""))")
    .NumberFormat = "mm/dd/yyyy"
  End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,935
Messages
6,122,337
Members
449,077
Latest member
Jocksteriom

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