Autofill until column (dynamic) is empty

Patcher

New Member
Joined
Sep 22, 2010
Messages
4
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))
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.

Forum statistics

Threads
1,224,613
Messages
6,179,898
Members
452,948
Latest member
Dupuhini

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