SUM stored as variable?

NinaE_11

Board Regular
Joined
Aug 18, 2020
Messages
67
Office Version
  1. 2016
Platform
  1. Windows
Hello!
I'm attempting to sum a column of values using the following code: ws.Range("G" & lastrow).Offset(2).Value = Application.Sum(Range("G7:G" & lastrow)) which seems to work as I expect. However, I am now looking to take the calculated value in this previous formula and use it in a new formula in Column H.

All of the constituent values in Column G, need to be summed, then divided by the sum to find the % of each constituent. In Excel terms "H7 =G7 / $G$100"

So to accomplish this, I believe I need to store the sum value in Column G as a variable to use in Column H formula - but I'm not sure how/when to declare it, and unclear how to incorporate it into Column H Code.

I hope I was able to demonstrate this properly. Any help would be appreciated - thank you!
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
would this help?
VBA Code:
For i = 7 To lastrow
    ws.Range("H" & i).Value = ws.Range("G" & i).Value / ws.Range("G" & lastrow).Offset(2).Value
Next i
 
Upvote 0
Try:

VBA Code:
  Dim tot As Double
  tot = Application.Sum(Range("G7:G" & lastrow))
  ws.Range("G" & lastrow).Offset(2).Value = tot
  
  With ws.Range("H7:H" & lastrow)
    .Formula = "=G7/" & tot
    .Value = .Value
  End With
 
Upvote 0
Try:

VBA Code:
  Dim tot As Double
  tot = Application.Sum(Range("G7:G" & lastrow))
  ws.Range("G" & lastrow).Offset(2).Value = tot
 
  With ws.Range("H7:H" & lastrow)
    .Formula = "=G7/" & tot
    .Value = .Value
  End With
Thank you so very much! This worked perfectly.
 
Upvote 1

Forum statistics

Threads
1,224,747
Messages
6,180,719
Members
452,995
Latest member
isldboy

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