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
 

Some videos you may like

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.

Scott Huish

MrExcel MVP
Joined
Mar 17, 2004
Messages
19,958
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
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?
 

lpsd

New Member
Joined
Sep 22, 2005
Messages
35
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.
 

tmcfadden

Board Regular
Joined
Aug 17, 2005
Messages
151
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
 

lpsd

New Member
Joined
Sep 22, 2005
Messages
35
tim,
i suppose this will work assuming i can substitute the A1:A3 with variables.
thanks
 

tmcfadden

Board Regular
Joined
Aug 17, 2005
Messages
151
Right. Example:
Code:
Sub SumPreviousRows(rngDest As Range, numRowsBefore As Integer)

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

End Sub
 

Watch MrExcel Video

Forum statistics

Threads
1,113,919
Messages
5,545,028
Members
410,647
Latest member
bernardazar
Top