VBA Sum R1C1 copy down

xcelnovice

Board Regular
Joined
Dec 31, 2011
Messages
81
Hi have the below code & want to manipulate it to copy down to the end of my data. Right now it goes down to Q3500 but I’d like it to be dynamic. Also if the structure of my code is funky m, mainly the last With statement I’m open to suggestions.

Code:
Sub Test()
Dim startRow As Long
Dim lastRow As Long

    startRow = 2

    lastRow = Cells(Cells.Rows.Count, 1).End(xlUp).Row

    ThisWorkbook.Worksheets("Page1_1").Cells.Copy

    With ThisWorkbook.Worksheets("DATA")
        .Range("A1").PasteSpecial Paste:=xlPasteValues

        With .Range("A4:C" & lastRow)
            .SpecialCells(xlCellTypeBlanks).FormulaR1C1 = "=R[-1]C"
            .Value = .Value
        End With
    End With

With ThisWorkbook.Worksheets (“DATA”)
         .Range(“Q4”).FormulaR1C1 = “SUM(RC[-12]:RC[-1])”
End With

End Sub
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
I am always leery about trying to figure out the details of what someone is trying to do by trying to decipher their code, especially when there code isn't exactly as they want it to (I don't see row 3500 hard-coded in your code anywhere, so I don't understand why it would stop there).

So, can you give us a detailed explanation in plain English of the structure of your data (being specific, regarding columns, rows, etc), and exactly what you would like the code to do?
 
Upvote 0
I apologize. Right now the formula only sums for 1 cell (Q4). I’d like to figure out how to change it so it would include all the rows (Q4 to the End of Data), which is currently down to Q3500. E-P is the months of the year & I am totaling them in Q. As far as I know the only part of the code that needs altered is below.

Code:
        With ThisWorkbook.Worksheets("DATA")
            .Range("Q4").FormulaR1C1 = "=SUM(RC[-12]:RC[-1])"
       
        End With

The above code sums data in E-P & puts the total in Q4. I’d like the code to do this in Col Q all the way down the sheet until the last row where there is data which is currently 3500 but will change. Hope this helps and I very much appreciate your help.

A B C D E-P Q
1

2

3

4 =SUM(E4:P4)
 
Upvote 0
It sounds like this may be what you are looking for:
Code:
        With ThisWorkbook.Worksheets("DATA")
            .Range("Q4:Q" & lastRow).FormulaR1C1 = "=SUM(RC[-12]:RC[-1])"
        End With
 
Upvote 0
You are welcome.
Glad I was able to help!:)
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,154
Members
453,021
Latest member
Justyna P

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