VBA to sumproduct selected cells

ferion132

New Member
Joined
Jul 11, 2018
Messages
2
Hi guys,

 
Would like to ask for some help on a simple VBA formula that would allow me to sumproduct my cells selected and to give the result in the cell below.

 
e.g.
I would highlight cells A1 to B3, and the VBA would sumproduct the selected cells (A1 to A3) and (B1 to B3) and give the result in the cell below at B4.
 
Reason why I need it to calculate for selected cells is because the data range constantly changes.
 
Thanks for your help!
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Welcome to the board.

You can account for varying rows by determining the last used row. See if this code helps:
Code:
Dim LR as Long
Const mySUMPRODUCT as String = "=SUMPRODUCT(A1:A@LR,B1:B@LR)"

LR = Range("A" & Rows.Count).End(xlUp).Row
Range("A" & LR + 1).Formula = Replace(mySUMPRODUCT, "@LR", LR)
 
Last edited:
Upvote 0
Welcome to the board.

You can account for varying rows by determining the last used row. See if this code helps:
Code:
Dim LR as Long
Const mySUMPRODUCT as String = "=SUMPRODUCT(A1:A@LR,B1:B@LR)"

LR = Range("A" & Rows.Count).End(xlUp).Row
Range("A" & LR + 1).Formula = Replace(mySUMPRODUCT, "@LR", LR)

Hi thanks for your reply, I am new to VBA, when I run the code it says compile error, constant expression required
 
Upvote 0
My error, try:
Code:
Dim LR as Long
Dim str as String

str = “=SUMPRODUCT(A1:A@LR,B1:B@LR)”
LR = Cell(Rows.Count, 1).End(xlUp).Row

Cells(LR + 1, 1).Formula = Replace(str, “@LR”, LR)
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,279
Members
452,630
Latest member
OdubiYouth

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