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

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney

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
 

Forum statistics

Threads
1,140,944
Messages
5,703,305
Members
421,290
Latest member
james90

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
Top