VBA to Fill Down with Dynamic Columns

FrenchCelt

Board Regular
Joined
May 22, 2018
Messages
214
Office Version
  1. 365
Platform
  1. Windows
Hello,

I'm writing a macro that will fill down a formula, but the column in question is dynamic, so I can't use the typical range that identifies the column by its letter designation. I have the code go so far as to insert the formula in the proper cell using a dynamic reference:

VBA Code:
Range("A2").End(xlToRight).Select
ActiveCell.Offset(rowOffset:=1, columnOffset:=-1).Activate
ActiveCell.FormulaR1C1 = _
    "=VLOOKUP(RC[-50],'4-Week Data'!C[-52]:C[-51],2,FALSE)"

And from there, I'm at a loss on how to fill down the formula to the bottom of the column (which should be anchored based on what is in Column A, since that is guaranteed to always have data all the way to the bottom).

Does anyone have any suggestions?
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
How about
VBA Code:
Dim lr As Long
lr = Range("A" & rows.Count).End(xlUp).Row
Range("A2").End(xlToRight).Offset(1, 1).Resize(lr - 2).FormulaR1C1 = _
    "=VLOOKUP(RC[-50],'4-Week Data'!C[-52]:C[-51],2,FALSE)"
 
Upvote 0
It places the formula and fills down two columns to the right of where I need it to go, but at least it goes down as far as I need it.
 
Upvote 0
Missed you had a minus sign in there, try
VBA Code:
Dim lr As Long
lr = Range("A" & rows.Count).End(xlUp).Row
Range("A2").End(xlToRight).Offset(1, -1).Resize(lr - 2).FormulaR1C1 = _
    "=VLOOKUP(RC[-50],'4-Week Data'!C[-52]:C[-51],2,FALSE)"
 
Upvote 0
Solution
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,063
Messages
6,122,935
Members
449,094
Latest member
teemeren

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