Insert SUM Formula using VBA (Range Varies)

W

William Horton

I have a macro that pulls a lot of text files and creates one Excel file. I
want the macro to insert SUM formulas into various places in the Excel file.
Could someone give me the VBA code to enter a SUM formula that calculates the
sum of a range that starts in row 8 and ends in 1 row up from where I am
putting the formula. Example - if I am putting the formula in cell B36 I
want the formula to SUM the range B8:B35. The starting row B8 will always be
the same but the ending row B35 will vary.

Thanks,
Bill Horton
 
P

PCLIVE

This will work for the example you've provided

Range("B35").FormulaR1C1 = "=SUM(R[-27]C:R[-1]C)"

HTH,
Paul
 
P

PCLIVE

You could also do it this way.

Range("B35").Formula = "=SUM(B8:B34)"


PCLIVE said:
This will work for the example you've provided

Range("B35").FormulaR1C1 = "=SUM(R[-27]C:R[-1]C)"

HTH,
Paul

William Horton said:
I have a macro that pulls a lot of text files and creates one Excel file.
I
want the macro to insert SUM formulas into various places in the Excel
file.
Could someone give me the VBA code to enter a SUM formula that calculates
the
sum of a range that starts in row 8 and ends in 1 row up from where I am
putting the formula. Example - if I am putting the formula in cell B36 I
want the formula to SUM the range B8:B35. The starting row B8 will
always be
the same but the ending row B35 will vary.

Thanks,
Bill Horton
 
T

Tom Ogilvy

good idea, but since row 8 is fixed and the location of the formula it not

ActiveCell.FormulaR1C1 = "=SUM(R8C:R[-1]C)"

might be a useful enhancement.

--
Regards,
Tom Ogilvy

PCLIVE said:
This will work for the example you've provided

Range("B35").FormulaR1C1 = "=SUM(R[-27]C:R[-1]C)"

HTH,
Paul

I have a macro that pulls a lot of text files and creates one Excel file.
I
want the macro to insert SUM formulas into various places in the Excel
file.
Could someone give me the VBA code to enter a SUM formula that calculates
the
sum of a range that starts in row 8 and ends in 1 row up from where I am
putting the formula. Example - if I am putting the formula in cell B36 I
want the formula to SUM the range B8:B35. The starting row B8 will always
be
the same but the ending row B35 will vary.

Thanks,
Bill Horton
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top