VBA code to sum dynamic range below a cell

LtCmdrData

Board Regular
Joined
Jan 24, 2018
Messages
58
Office Version
  1. 365
Platform
  1. Windows
I have 2 columns of data - first column is item and the second column is the count of that item. The number of rows will vary each time the report is run. I have vba code to place "Grand Total" in row 4 of column Q. I need vba code to sum the total of the second column starting in row 5 and going down a dynamic range and place that total in row 4 of column R so it lines up with the Grand Total. Is there a way to utilize CurrentRegion? I appreciate any help someone could offer. Thanks.
Book1
QR
4Grand Total SUM goes here
5A959,973
6B338,125
7C241,641
8D156,772
9E50,232
10F37,352
11H25,080
12I18,785
13J13,223
14K11,204
15L9,735
16M6,361
17N5,748
18O4,266
19P4,214
20Q4,180
21R4,145
22S3,956
Sheet1
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Here is one way:
VBA Code:
Sub AddGrandTotal()
    
    Dim lr As Long
    
'   Find last row with data in column R
    lr = Cells(Rows.Count, "R").End(xlUp).Row
    
'   Put sum formula in cell R4
    Range("R4").Formula = "=SUM(R5:R" & lr & ")"
    
End Sub
 
Upvote 0
try this

VBA Code:
Range("R4") = Application.Sum(Range("R5:R" & Cells(Rows.Count, "R").End(xlUp).Row))
 
Upvote 0
Solution
Just want to point out a difference in the two replies (which may or may not matter to you).
My solution will place a SUM formula in cell R4.
Dave's solution will just place the value of the sum (hard-coded number) in cell R4.

You may not care, unless you need it to to be dynamic, and change automatically if the existing values in column R may change (and you want the sum to change accordingly).
But you should just be aware of the difference, and do whatever suits your needs best.
 
Upvote 0
Just want to point out a difference in the two replies (which may or may not matter to you).
My solution will place a SUM formula in cell R4.
Dave's solution will just place the value of the sum (hard-coded number) in cell R4.

You may not care, unless you need it to to be dynamic, and change automatically if the existing values in column R may change (and you want the sum to change accordingly).
But you should just be aware of the difference, and do whatever suits your needs best.
Thank you for taking the time to point out the difference to me. In this instance the other solution works because hard-coding is fine for this application but now I have an alternative if circumstances change.
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,209
Members
453,022
Latest member
RobertV1609

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