I want to have generic code that can be used to convert dates on any spreadsheet in any column from m/d/yyyy to YYYYMMDD. I have everything coded except the autofill. The code below (which I'm still working on as I want it to insert 3 columns) sort of works. The user will highlight the date column they want to convert. Then they run the macro. It separates the column into month day and year based on "/". Then in the next column it puts them back together in YYYYMMDD and adds any leading zeroes using an array. I found the last three lines of code online, but it's going too far. The problem is it fills until row 65536. If I'm including this in another macro, this can add to the time it takes to finish. Basically what I need is the autofill to fill until RC-3 is empty. I'm new to Visual Basic so I know just enough to be dangerous. Any ideas?
Thanks!
Selection.TextToColumns Destination:=Cells(1, ActiveCell.Column), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar _
:="/", FieldInfo:=Array(Array(1, 1), Array(2, 1), Array(3, 1)), _
TrailingMinusNumbers:=True
Range(Selection, Selection.Columns(3)).EntireColumn.Select
Selection.NumberFormat = "General"
Selection.EntireColumn.AutoFit
ActiveCell.Offset(1, 3).Select
ActiveCell.FormulaR1C1 = _
"=IF(AND(LEN(RC[-3])=1,LEN(RC[-2])>1),RC[-1]&""0""&RC[-3]&""""&RC[-2],IF(AND(LEN(RC[-3])>1,LEN(RC[-2])=1),RC[-1]&""""&RC[-3]&""0""&RC[-2],IF(AND(LEN(RC[-3])=1,LEN(RC[-2])=1),RC[-1]&""0""&RC[-3]&""0""&RC[-2],RC[-1]&""""&RC[-3]&""""&RC[-2])))"
'MM IS RC-3, DD IS RC-2, YY IS RC-1
Dim rng1 As Range
Set rng1 = Selection
rng1(1, 1).AutoFill Destination:=Range(rng1(1, 1), rng1(1, 1).End(xlDown))
Thanks!
Selection.TextToColumns Destination:=Cells(1, ActiveCell.Column), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar _
:="/", FieldInfo:=Array(Array(1, 1), Array(2, 1), Array(3, 1)), _
TrailingMinusNumbers:=True
Range(Selection, Selection.Columns(3)).EntireColumn.Select
Selection.NumberFormat = "General"
Selection.EntireColumn.AutoFit
ActiveCell.Offset(1, 3).Select
ActiveCell.FormulaR1C1 = _
"=IF(AND(LEN(RC[-3])=1,LEN(RC[-2])>1),RC[-1]&""0""&RC[-3]&""""&RC[-2],IF(AND(LEN(RC[-3])>1,LEN(RC[-2])=1),RC[-1]&""""&RC[-3]&""0""&RC[-2],IF(AND(LEN(RC[-3])=1,LEN(RC[-2])=1),RC[-1]&""0""&RC[-3]&""0""&RC[-2],RC[-1]&""""&RC[-3]&""""&RC[-2])))"
'MM IS RC-3, DD IS RC-2, YY IS RC-1
Dim rng1 As Range
Set rng1 = Selection
rng1(1, 1).AutoFill Destination:=Range(rng1(1, 1), rng1(1, 1).End(xlDown))