VBA code to autofill formula to last cell in row

lwebbie

New Member
Joined
Feb 28, 2017
Messages
24
I am trying to write a VBA code that will autofill the formula to the last active cell in the column. The range varies each week, sometimes there will be 3 rows of data other times there will be more. Ideally the code is dynamic and will adjust without me having to manually update the code.

Right now the range is Range("R8:R10").

Sub Macro2Device()
ActiveCell.FormulaR1C1 = _
"=VLOOKUP(RC[-11],XHSCompletedOrders.xlsx!R2C7:R3341C21,2,FALSE)"
Selection.AutoFill Destination:=Range("R8:R10")
Range("R8:R10").Select
End Sub

Thanks
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Code:
Sub Macro2Device()
Dim rng As Range
 With ActiveSheet
     Set rng = .Range("R8", .Cells(Rows.Count, "R").End(xlUp))
 End With
 ActiveCell.FormulaR1C1 = _
 "=VLOOKUP(RC[-11],XHSCompletedOrders.xlsx!R2C7:R3341C21,2,FALSE)"
 Selection.AutoFill Destination:=rng
 End Sub
 
Last edited:
Upvote 0
That added the formula to all the cells in column R with R8 being the last cell (so cells R1:R8 now have the formula). I need the formula to always start in cell R8 and then paste down however many rows of data there are. If it needs a cell to reference for data it can reference column Q.
 
Upvote 0
Hi

you could try this

Code:
Sub loop_formula()
lastrow = Range("Q" & Rows.Count).End(xlUp).Row
    For a = 8 To lastrow
        With Range("a" & a)
            .FormulaR1C1 = "=VLOOKUP(RC[-11],XHSCompletedOrders.xlsx!R2C7:R3341C21,2,FALSE)"
        End With
    Next
End Sub

test it on a copy of your workbook

dave
 
Last edited:
Upvote 0
Maybe

Code:
Sub MM1()
Range("R8:R" & Cells(Rows.Count, "Q").End(xlUp).Row).Formula = "=VLOOKUP(G7,XHSCompletedOrders.xlsx!$G$2:$U$3341,2,FALSE)"
End Sub
 
Last edited:
Upvote 0
I see Michael has made it simple, but this is just for the record to autofill.

Code:
Sub Macro2Device()
Dim rng As Range
 With ActiveSheet
     Set rng = .Range("R8", .Cells(Rows.Count, "Q").End(xlUp).Offset(, 1))
 End With
 ActiveCell.FormulaR1C1 = _
 "=VLOOKUP(RC[-11],XHSCompletedOrders.xlsx!R2C7:R3341C21,2,FALSE)"
 Selection.AutoFill Destination:=rng
 End Sub
 
Upvote 0

Forum statistics

Threads
1,214,611
Messages
6,120,513
Members
448,967
Latest member
screechyboy79

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