VBA Autofill Formula to last row w data

Littlemalky

Board Regular
Joined
Jan 14, 2011
Messages
223
Hey, so I have this line of code that seems like it should work, I don't get any kind of error when I run the macro; however, when I check my file the only part of it that worked was the copying and pasting the formula into the single cell. Even though I have the autofill language in there, it doesn't seem to be applying it to the column. Here's what I have:
Code:
Range("D2").Copy _
        Destination:=Workbooks("Fulfillment " & Format(Date, "yyyymmdd") & " - Domestic" & ".xlsx").Worksheets("detail w add").Range("S2")
    With Range("S2")
        .AutoFill Destination:=Range("S2", Cells(Cells(Rows.Count, 20).End(xlUp).Row, 19))
    End With
I just need it to autofill down to the last row with data in it. I have data in columns A:V if that's relevant, but I only want to autofill column "S".
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Perhaps

Code:
Range("D2").Copy _
        Destination:=Workbooks("Fulfillment " & Format(Date, "yyyymmdd") & " - Domestic" & ".xlsx").Worksheets("detail w add").Range("S2")
    With Workbooks("Fulfillment " & Format(Date, "yyyymmdd") & " - Domestic" & ".xlsx").Worksheets("detail w add").Range("S2")
        .AutoFill Destination:=Range("S2", .Cells(Cells(Rows.Count, 20).End(xlUp).Row, 19))
    End With
 
Upvote 0
You almost have it. When using Range() in that fashion (designating the first cell in the range in the first argument, the last cell in the range in the second argument), you need to use the Cells() function inside the range in each argument:

Code:
Range("D2").Copy _
        Destination:=Workbooks("Fulfillment " & Format(Date, "yyyymmdd") & " - Domestic" & ".xlsx").Worksheets("detail w add").Range("S2")
    With Range("S2")
        .AutoFill Destination:=Range([COLOR=red][B]Cells(2,19),[/B][/COLOR]Cells(Cells(Rows.Count, 20).End(xlUp).Row, 19))
    End With
 
Upvote 0
VoG, that didn't work, it gave me an error.

MrKowz, that makes sense, and I applied that exact line of code. I didn't get any kind of error, but when I look at my file, it still hasn't copied the formula all the way down. I feel like there is one little tweak I'm missing.
 
Upvote 0
Maybe

Code:
Range("D2").Copy _
        Destination:=Workbooks("Fulfillment " & Format(Date, "yyyymmdd") & " - Domestic" & ".xlsx").Worksheets("detail w add").Range("S2")
    With Workbooks("Fulfillment " & Format(Date, "yyyymmdd") & " - Domestic" & ".xlsx").Worksheets("detail w add").Range("S2")
        .AutoFill Destination:=Range(.Cells(2, 19), .Cells(Cells(Rows.Count, 20).End(xlUp).Row, 19))
    End With
 
Upvote 0
Try

Code:
Range("D2").Copy _
        Destination:=Workbooks("Fulfillment " & Format(Date, "yyyymmdd") & " - Domestic" & ".xlsx").Worksheets("detail w add").Range("S2")
    With Workbooks("Fulfillment " & Format(Date, "yyyymmdd") & " - Domestic" & ".xlsx").Worksheets("detail w add")
        LR = .Range("A" & Rows.Count).End(xlUp).Row
        .Range("S2").AutoFill Destination:=.Range(.Cells(2, 19), .Cells(LR, 19))
    End With
 
Last edited:
Upvote 0
Ahh - the Range references weren't linked to that secondary workbook:

Code:
Range("D2").Copy _
        Destination:=Workbooks("Fulfillment " & Format(Date, "yyyymmdd") & " - Domestic" & ".xlsx").Worksheets("detail w add").Range("S2")
With Workbooks("Fulfillment " & Format(Date, "yyyymmdd") & " - Domestic" & ".xlsx").Worksheets("detail w add")
    .Range("S2").AutoFill Destination:=.Range(.Cells(2, 19), .Cells(Cells(Rows.Count, 20).End(xlUp).row, 19))
End With

or to make the code a bit easier to read and manipulate:

Code:
Dim wb As Workbook
Dim ws As Worksheet
Set wb = Workbooks("Fulfillment " & Format(Date, "yyyymmdd") & " - Domestic" & ".xlsx")
Set ws = wb.Worksheets("detail w add")
Range("D2").Copy _
        Destination:=ws.Range("S2")
With ws
    .Range("S2").AutoFill Destination:=.Range(.Cells(2, 19), .Cells(Cells(Rows.Count, 20).End(xlUp).row, 19))
End With
 
Upvote 0
This is getting embarrassing. I feel like these should all be working because they are logical...yet again, still no error, but it's not applying it. I'm so confused...I feel like it's something simple I'm missing.

MrKowz, I just tried your:
Code:
Dim wb As Workbook
Dim ws As Worksheet
Set wb = Workbooks("Fulfillment " & Format(Date, "yyyymmdd") & " - Domestic" & ".xlsx")
Set ws = wb.Worksheets("detail w add")
Range("D2").Copy _
        Destination:=ws.Range("S2")
With ws
    .Range("S2").AutoFill Destination:=.Range(.Cells(2, 19), .Cells(Cells(Rows.Count, 20).End(xlUp).row, 19))
End With
 
Upvote 0
Maybe

Rich (BB code):
.Range("S2").AutoFill Destination:=.Range(.Cells(2, 19), .Cells(.Cells(Rows.Count, 20).End(xlUp).Row, 19))
 
Upvote 0

Forum statistics

Threads
1,224,505
Messages
6,179,152
Members
452,891
Latest member
JUSTOUTOFMYREACH

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