VBA to copy/paste dynamic formula across range?

trillium

Board Regular
Joined
Aug 9, 2010
Messages
63
Hi
I'm trying to figure out code to copy and paste a set of formulas across a bunch of columns and then down to the end of the range. I can figure out that part with VBA, but I can't figure out how to set the formulas so that the cell references don't get messed up in VBA.

It's just a simple COUNTIFS and SUMIFS formulas but the column references need to stay and some sell references need to move either right or down.

=COUNTIFS('Order Export'!$B:$B,'Customer List'!$A2,'Order Export'!$L:$L,'Customer List'!K$1)

So that when the macro moves the formula over to cover 5 columns, the $B:$B/$L:$L stay the same but A2 will also stay the same for the row, but change to A3 in the next and K1 will become L1, M1, N1,O1... and next year when 2019 data is entered the new column P will have the formula...

I want to use some type of autofill with dynamic range script to move the formulas and the corresponding references over.

Any thoughts??
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Paste to the whole range at once in one line, for example :

[A1:D10].Formula="=COUNTIFS('Order Export'!$B:$B,'Customer List'!$A2,'Order Export'!$L:$L,'Customer List'!K$1)"
 
Upvote 0
That's brilliant!

Can I ask, how to do make this part of the code dynamic? [A1:D10]?

In my case it's K2:P2 and then R2:W2 with totals in columns Q & X. But next year there will be another year's worth of data, so the formulas will be pasted into K2:Q2, R2:W2 with totals in columns R&Y.

I tried a few things to make that part dynamic as well but not working.

 
Upvote 0
For columns K:P and R:W from row 2 to the last data row (using column A to determine the last row) :
Code:
Dim lr&, rng As Range
lr = Cells(Rows.Count, "A").End(xlUp).Row
Set rng = Range("K2:P" & lr & ",R2:W" & lr)
rng.Formula = "=COUNTIFS('Order Export'!$B:$B,'Customer List'!$A2,'Order Export'!$L:$L,'Customer List'!K$1)"
And similarly for your Q & X formulas.
 
Upvote 0
Thanks - I'm good with finding the last row and changing the variable there to make the formula extend farther down. What I can't figure out is how to tell Excel to copy the formula over one column further. How do I make "K" and "P" dynamic so that next year when another year of data is added it pastes the countif formula from K to Q?
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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