Sum dynamic ranges

RobbertH

Active Member
Joined
Apr 29, 2008
Messages
310
Gurus,

I'm trying to put a sum formula is some cells by running a macro. The formula that has to be put into the cells sums a range that is located in another workbook. The name of this workbook will vary. The workbook is open at the time the code should put the sum formula into the required cells.
How do i put together the vba code?
an example of the formula that has to be writen into the cell is:
Code:
"=-SUM([Omzet2011tot_02.xls]Maand!R33C2:R33C25)"
Thanks for any help!
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Here's how I would go about it. I don't know what your code looks like, but hopefully a general idea will help you.

Since the workbook varies the only thing I can think of is that you have the varying workbook open and active when you run the macro and then set it as a variable at the start like this.

Code:
set varBook = ActiveWorkbook
set varSheet = activesheet

Now you can reference the workbook and worksheet as needed. Then to get it into the formula you can do something like this

Code:
Cells(1,1).FormulaR1C1 = "=-SUM([" & varBook.name & "]" & varSheet.name & "!R33C2:R33C25)"

I hope this is the direction you were looking for.
 
Upvote 0

Forum statistics

Threads
1,224,595
Messages
6,179,799
Members
452,943
Latest member
Newbie4296

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