Sum formula with variable

camandab

Board Regular
Joined
Feb 10, 2010
Messages
79
Excel 2002

I am trying to reference a range of cells using a variable and then put the sum of that range in another cell. Below is my code:

Code:
Dim Nett As Range
   Dim MRange As Range
   Dim MFormula As String
   
    
   Set Nett = Cells.Find(what:="nett.", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
        :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
        False, SearchFormat:=False).Offset(1, 0)
    Set MRange = Range(Nett, Selection.End(xlDown))
    [COLOR=Blue]MFormula = "=Sum(MRange)"[/COLOR]
    Range("L1772").FormulaR1C1 = MFormula
The line in blue is where I'm having trouble. Once the macro finishes and I look in cell L1772, all is says is =SUM(MRange). I've tried the formula as
MFormula = "=Sum(" & MRange & ") but get an error.

I can't figure out the correct syntax or if I'm even able to create this formula.

Once I figure out this formula, I'll need to add the variable to another formula.

FormulaR1C1 = "=SUM(R3C13:R[-2]C)" <--the original formula
FormulaR1C1 = "=SUM(R3C13:R[-2]C) + MRange" <--formula I'd like to end up with

Any help will be much appreciated! :)
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Code:
Dim Nett As Range
   Dim MRange As Range
   
    
   Set Nett = Cells.Find(what:="nett.", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
        :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
        False, SearchFormat:=False).Offset(1, 0)
    Set MRange = Range(Nett, Selection.End(xlDown))
    Range("L1772").Formula = "=Sum(" & MRange.Address & ")"
 
Upvote 0
Perfect! I got past the part with the formula but now I'm getting Run-time error 1004: Application-defined or object-defined error on this line:

Range("L1772").FormulaR1C1 = MFormula


I have MFormula declared as a string so I can't figure out why I'm getting the error. Any ideas?
 
Upvote 0
You can use R1C1 like so

Code:
Dim Nett As Range
   Dim MRange As Range
   
    
   Set Nett = Cells.Find(what:="nett.", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
        :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
        False, SearchFormat:=False).Offset(1, 0)
    Set MRange = Range(Nett, Selection.End(xlDown))
    Range("L1772").FormulaR1C1 = "=Sum(" & MRange.Address(,,xlR1C1) & ")"
 
Upvote 0

Forum statistics

Threads
1,215,465
Messages
6,124,982
Members
449,201
Latest member
Lunzwe73

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