Enter SUM formula using VBA

General Ledger

Active Member
Joined
Dec 31, 2007
Messages
460
Dear All,

I am trying to put a formula into a cell using VBA but am having trouble with the range of cells. I am using Excel 2003.

I have a column of amounts with some blanks mixed in. I know my first cell in the range is always row 2 of the column of the current active cell. I know the last cell in the range is always cell directly above the current active cell. How do I pass this range to get VBA to enter a formula?

For example, column C has amounts. Cell C1 has a label. Cell C2 is the beginning of my range, which may or may not have an amount. The active cell is C100. In the active cell, I want to use VBA to enter the formula =SUM(C2:C99) If the active cell is C345, I want the formula to be =SUM(C2:C344)

I tried to use ActiveCell.FormualR1C1 = "=SUM(R[]C[]:R[]C[])" However, I can not figure out how to enter/show the beginning cell of the range, which is always the current column row 2. The ending cell reference would be R[-1]C[0]

Please help make this Holiday Season joyfull.

Thanks,

General Ledger
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Try...

Code:
ActiveCell.FormulaR1C1 = "=SUM(R2C:R[-1]C)"
 
Upvote 0
If you actually want relative addresses, perhaps:

Code:
Sub test()
Dim x As Long, y As Long
x = ActiveCell.Column: y = ActiveCell.Row
ActiveCell.Formula = "=SUM(" & Cells(2, x).Address(0, 0) & ":" & Cells(y - 1, x).Address(0, 0) & ")"
End Sub
 
Upvote 0
Domenic: Your answer worked perfectly!!

Hot Pepper: Your answer is a great example of how to string together several elements to get the desired formula. This helped me to see how far you can go push the envelope on this topic.

Best regards to you both,

General Ledger
 
Upvote 0

Forum statistics

Threads
1,214,787
Messages
6,121,565
Members
449,038
Latest member
Guest1337

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