VBA Sum formula

lpsd

New Member
Joined
Sep 22, 2005
Messages
35
hello all,
i need help with summing rows and columns of various lengths using VBA. my code puts the cursor on say cell D10. in this cell i want the formula for summing the row DX to D9. the x is variable and is stored in SUMCOLSTART.

i don't want the result in cell D10. i want the formula (for example =SUM(Dx:D9)) in the cell so that i can copy it down several rows.

i've been playing with several pieces of code but all put the number in D10, not the formula

thanks,
lpsd
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
If you use VBA to put a formula in a cell, it will still give you the result of the formula, but you don't want a number in the cell, can you please clarify what you want?
 
Upvote 0
i'm looking for the actual formula to reside in the cell. then i can copy this forumula down several rows. so all i want in the cell (D10) is something like =sum(dx:d9). the x is a variable.
 
Upvote 0
When you set the cell, you have to use someting like:
Code:
Sheet1.Range("A4") = "=SUM(A1:A3)"

Otherwise you aren't putting the formula in.

-Tim
 
Upvote 0
tim,
i suppose this will work assuming i can substitute the A1:A3 with variables.
thanks
 
Upvote 0
Right. Example:
Code:
Sub SumPreviousRows(rngDest As Range, numRowsBefore As Integer)

rngDest = "=SUM(" & rngDest.Offset(-numRowsBefore).Address & ":" & rngDest.Offset(-1).Address & ")"

End Sub
 
Upvote 0

Forum statistics

Threads
1,213,482
Messages
6,113,913
Members
448,532
Latest member
9Kimo3

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