Forumula error in vba

dantheman9

Board Regular
Joined
Feb 5, 2011
Messages
175
HI,

can anyone help with this one?

Code:
ActiveCell.Formula = "=SUM((Range(Cells(0, k - 2))):(Range(Cells(0,1 +(k+1))))) & ""M"""


k is a varable integer

thanks
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Code:
ActiveCell.FormulaR1C1 = "=SUM(RC[-2]:RC[-1])

...but the first cell location is not always the same (hence using k as a varable)

?
 
Upvote 0
What exactly is Range(Cells(0, k - 2))):.... etc meant to refer to?

Even if you were trying to use that to get the address of a range to sum it wouldn't work because there is no row 0.:)
 
Upvote 0
arh...that might well be where im going wrong then!!

Im trying to do the following;

=SUM(current row, Varable column : Current Row, -1 Column)
 
Upvote 0
You might not actually need to do any of that.

Have you tried just using what's been recorded?

The formula in that code uses R1C1 relative referencing so the actual range that will be summed will be based on where the formula is entered.

Perhaps this might illustrate what I mean.

If I select D8 and run that code the resultant formula is SUM(B8:C8).

If I select C20 the formula is SUM(A20:B20).

So wherever you enter the formula the sum is of the 2 cells to the left.
 
Upvote 0
HI Norie,

Yes i see what you mean, problem is that number of columns between the the location of the forumla changes (and so can vary from 2 columns to 10) all of which need to be included in the SUM.

Cheers
 
Upvote 0
What exactly are you trying to sum?

This should let you specify the column (by number) in the formula.
Code:
ActiveCell.FormulaR1C1 = "=SUM(RC" & K & ":RC" & (K - 1) & ")"
 
Upvote 0

Forum statistics

Threads
1,224,575
Messages
6,179,637
Members
452,934
Latest member
Jdsonne31

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