How to use SUM, MAX or MIN on a variable column range

NinaE_11

Board Regular
Joined
Aug 18, 2020
Messages
54
Office Version
  1. 2016
Platform
  1. Windows
Hello,
I'm attempting to do some simple worksheetfunctions on a column of values (# of values is likely to change and needs to be dynamic) and to place that SUM (or MAX , MIN) in an offset cell below the column. Data will always start in D7. Here's what I have so far:

'Create first and last row for formulas
lastrow = Sheets("SC").Cells(Sheets("SC").Rows.Count, "B").End(xlUp).Row
firstrow = Sheets("SC").Cells(lastrow, "B").End(xlUp).Row

Dim lastrow As Long

Range("D7").End(xlDown).Offset(4, 0).Select
ActiveCell.FormulaR1C1 = "=SUM(D7:D & lastrow)"

My result comes back with a #NAME, so I'm not sure I'm using correct way to find lastrow, or if something is wrong with SUM formula.

Thank you!
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Hi & welcome to MrExcel.
How about
VBA Code:
Range("D7").End(xlDown).Offset(4, 0).FormulaR1C1 = "=sum(r7c:r[-4]c)"
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0
Would there be a way I can 'set' that value of the above SUM calculation, so I can refer to it in other calculations?
 
Upvote 0
What exactly do you mean?
 
Upvote 0
I just ran your SUM command, which I will then need to take each of the data points of that column and divide by the SUM to get a % of each data point in a different column. (i.e. Joe's Revenue / Total Revenue = % of Revenue). Do I use the entire code you just provided to reference it in another calculation?
 
Upvote 0
Is this what you mean
VBA Code:
Sub NinaE()
   Dim NxtRw As Long

   NxtRw = Range("D7").End(xlDown).Row
   Range("D" & NxtRw).Offset(4).FormulaR1C1 = "=sum(r7c:r[-4]c)"
   Range("E7:E" & NxtRw).FormulaR1C1 = "=rc[-1]/r" & NxtRw + 4 & "c[-1]"
End Sub
 
Upvote 0
Glad to help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,819
Messages
6,121,741
Members
449,050
Latest member
excelknuckles

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