Multiply 2 columns and then sum the totals VBA

aheverett1

New Member
Joined
May 23, 2017
Messages
16
Hello,

Is there a way to take multiply say A1 * B1, A2 * B2, A3 * B3 then put the extended totals add them together and add those up?

Like this but without using a third column and just putting the total of 196 in a cell?

QTYPriceExt price
16666
18585
315
45
Total
196

<colgroup><col style="width:48pt" width="64" span="3"> </colgroup><tbody>
</tbody>
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
try this:



Code:
Sub NoHelper()
    Dim i As Long, lr As Long
    Dim x As Double, y As Double
    lr = Range("A" & Rows.Count).End(xlUp).Row
    x = 0
    For i = 2 To lr
        y = Range("A" & i) * Range("B" & i)
        x = x + y
    Next i
    Range("A" & lr + 1) = "total"
    Range("B" & lr + 1) = x




End Sub
 
Upvote 0
try the following


Excel 2010
AB
1QTYPrice
2166
3185
4315
5Total196
6a
Cell Formulas
RangeFormula
B5=SUMPRODUCT(A2:A4,B2:B4)
 
Upvote 0
OK, should have been a little more specific. My first range actually starts B6 and the other range starts at I6

Thought this would fix it, but it did not.

Private Sub CommandButton1_Click()
Dim i As Long, lr As Long
Dim x As Double, y As Double
lr = Range("B6" & Rows.Count).End(xlUp).Row
x = 0
For i = 2 To lr
y = Range("B6" & i) * Range("I6" & i)
x = x + y
Next i
Range("B6" & lr + 1) = "total"
Range("I6" & lr + 1) = x
End Sub


Also, how would i output the total or the loop into cell "I1"?
 
Upvote 0
Amended Code to fit your better description:

Code:
Option Explicit
Sub Nohelper()
    Dim i As Long, lr As Long
    Dim x As Double, y As Double
    lr = Range("B" & Rows.Count).End(xlUp).Row
    x = 0
    For i = 6 To lr
        y = Range("B" & i) * Range("I" & i)
        x = x + y
    Next i
    Range("I1") = x


End Sub
PLease use code tags when posting VBA
 
Upvote 0

Forum statistics

Threads
1,216,077
Messages
6,128,684
Members
449,463
Latest member
Jojomen56

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