VBA - Summing a dynamic gap range between two named ranges?

RockandGrohl

Well-known Member
Joined
Aug 1, 2018
Messages
801
Office Version
  1. 365
Platform
  1. Windows
Hi all, can't even figure out how to phrase this in Google or ChatGPT to get something that is nice and clean.

I have a sheet where in column J, there are costs, and sub-totals for those costs.

Each subtotal is a named range, for instance:
"DirectCostSum" is J23
"IndirectCostSum" is J31

What I'd like to do is for IndirectCostSum cell value to be a sum of J24:J30.

These subtotals are dynamically placed based on how many products I am summing, so the gap between the named ranges I want to sum is always changing.

What I'm after is a nice, one line chunk of code which will be like..

VBA Code:
range("IndirectCostSum").formular1c1 = "=SUM( [DirectCostSum + 1],[IndirectCostSum -1]),

That's the best way I can describe it lol. Hopefully that makes sense.
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Does the actual formula itself need to be dynamic, or do you just need to calculate the right range at runtime of the code?
 
Upvote 0
Does the actual formula itself need to be dynamic, or do you just need to calculate the right range at runtime of the code?
Just at runtime, I'm intending to replace the formula with values at the end of the process. It just needs to calculate the nice range.

I can of course do it a sloppy way by just getting the address of each named range, saving them as their own variable and then doing a cells formula instead but this is many many lines and I reckon there's away to do it cleanly.

Chat GPT gives me this which looks like it might work but I get the 1004 error.

VBA Code:
Range("IndirectCostSum").Formula = "=SUM(" & Range("DirectCostSum").Offset(1, 0).Address & ":" & Range("IndirectCostSum").Offset(-1, 0).Address & ")"
 
Upvote 0
That should work. Or you could use:

VBA Code:
Range("IndirectCostSum").FormulaR1C1 = "=SUM(" & Range("DirectCostSum").Offset(1, 0).Address(referencestyle=xlr1c1) & ":R[-1]C)"
 
Upvote 0
That should work. Or you could use:

VBA Code:
Range("IndirectCostSum").FormulaR1C1 = "=SUM(" & Range("DirectCostSum").Offset(1, 0).Address(referencestyle=xlr1c1) & ":R[-1]C)"
Unfortunately that still gives Application or Object defined error (1004)

This is usually where the named ranges aren't correct but I've verified they're spot on.

FYI This is still running inside the Worksheet_Activate module so I added the references as such:

VBA Code:
ios.Range("IndirectCostSum").FormulaR1C1 = "=SUM(" & ios.Range("DirectCostSum").Offset(1, 0).Address(ReferenceStyle = xlR1C1) & ":R[-1]C)"
 
Upvote 0
Are those ranges direct cell references?

Just noticed I missed a colon when typing the code earlier:

Code:
ios.Range("IndirectCostSum").FormulaR1C1 = "=SUM(" & ios.Range("DirectCostSum").Offset(1, 0).Address(ReferenceStyle:=xlR1C1) & ":R[-1]C)"
 
Upvote 0
Solution
I just updated my last post.
 
Upvote 0
It's not the code - I just tested it and it works fine.
 
Upvote 0

Forum statistics

Threads
1,223,881
Messages
6,175,161
Members
452,615
Latest member
bogeys2birdies

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