VBA to add SUM formula to cell using 2 variables, and also name the cell

Pianostool

New Member
Joined
Dec 4, 2008
Messages
20
Office Version
  1. 365
Platform
  1. Windows
Hi folks.

I'm trying to put a SUM formula into cell B2 via VBA.

The range of the SUM can vary depending on inputs, so I can't hard cade any cell references into it.

So, the formula I need in B2 is something like:

=SUM(Variable1:Variable2)

Variable1 will be something like "Range("A" & Position1.Row).Offset(2)"

Variable2 will be something like "Range("A" & Position2.Row).Offset(2)"

Position1 and Position2 are cells defined via a search for specific texts. So I have them already defined and working.

Also, once that cell has the formula in it from the code, I would like that cell named as SENDTOTAL.

Any help would be appreciated.

Thanks in advance.
 
I run a demo, with the 2 pos are A4 and A20:
VBA Code:
Option Explicit
Sub Sumrng()
Dim position1 As Range, position2 As Range
Set position1 = Range("A4")
Set position2 = Range("A20")
Set position1 = position1.Offset(2)
Set position2 = position2.Offset(2)
With Range("B2")
    .Formula = "=SUM(" & position1.Address & ":" & position2.Address & ")"
    .Name = "SENDTOTAL"
End With
End Sub

with your position1 and position are defined:

VBA Code:
With Range("B2")
    .Formula = "=SUM(" & position1.Address & ":" & position2.Address & ")"
    .Name = "SENDTOTAL"
End With
 
Upvote 0
Solution
Awesome @bebo021999 - I was able to use your info to get it working.

Had some other adjustments to do because of my data, but your whole principle worked really well.

Thank you.
 
Upvote 0

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