VBA - Using cell value as a part of a range and dragging and dropping.

PGD15

Board Regular
Joined
Aug 5, 2016
Messages
137
Hi there,

I am a bit of a newb when it comes to VBA. I've tried many different options and nothing worked. I cannot think of a way to get this to work. Would appreciate any help:

Aim:
I wish to paste a sum formula in Sheet1, Column R starting in Row 2. which sums the previous 4 columns (simple enough). However, I wish for my VBA code to drag and drop this sum formula down a row number which is variable based on the value in Sheet2 A1.

Basically something like worksheets("Sheet1").range("R2").formula = "=sum(N2:Q2)" then I want to repeat this down to the value of Sheet2 A1 which will always be a number (this cell has a formula in it so will be variable).
Ie i wish to "drag an drop" it down.

OR
Everything is the same but ignoring the value in Sheet2 A1, i wish for the formula to be dragged down to the last row where there is a data in column Q.

In simple R2 should have the formula =sum(N2:Q2). R3 =sum(N3:Q3) and so on but repeated down to a row where i don't know where it will end.

Hope this makes sense, any suggestions would be appreciated.


EDIT:
I think its something like the below but i don't know how to change the end value of the range from a predefined value to a variable or where data is last present in the column Q (RC-1)

ActiveCell.FormulaR1C1 = "=SUM(RC[-4]:RC[-1])"
Range("R1").Select
Selection.AutoFill Destination:=Range("R1:R2255"), Type:=xlFillDefault
Range("R1:R2255").Select
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
No autofill needed.
Try this single line of code:
VBA Code:
Range("R1:R" & Cells(Rows.Count, "Q").End(xlUp).Row).FormulaR1C1 = "=SUM(RC[-4]:RC[-1])"
 
Upvote 0
Solution
You are welcome.
Glad I was able to help!
 
Upvote 0

Forum statistics

Threads
1,223,712
Messages
6,174,033
Members
452,542
Latest member
Bricklin

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