sumproduct vba

Vanda_a

Well-known Member
Joined
Oct 29, 2012
Messages
934
Hello all

I am new to VBA, I am trying to learn it.

First thing I have tried was fail, please kindly advise
Code:
Sub Learning
Cells(6, 11) = Application.WorksheetFunction.Sumproduct((Sheet2.Range("A2:A10") = Range("B6")) * (Sheet2.Range("B1") = Range("K5")) * Sheet2.Range("B2:B10"))
End Sub
Thank you in advance
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Can you post the formula as it would look on a worksheet?
 
Upvote 0
Sumproduct in VBA doesn't work the same as it does in a formula.
It can't do the (range=criteria) expressions to creaate arrays.

Plus your ranges aren't the same size anyway
What is the intention of (Sheet2.Range("B1") = Range("K5")) ?

Do you have a working sumproduct formula in a Cell Formula?
Can you post it, and we can try to make it work in VBA.
 
Upvote 0
Sumproduct is a worksheet function and not VBA according to internet posts.
 
Upvote 0
What is the intention of (Sheet2.Range("B1") = Range("K5")) ?

Can you post it, and we can try to make it work in VBA.
Sheet2 B1 = sheet1 K5.

Here it is
Code:
=SUMPRODUCT((Sheet2!$A$2:$A$10=Sheet1!B6)*(Sheet2!B1=Sheet1!K5)*Sheet2!$B$2:$B$10)
Thank you
 
Upvote 0
Try

Cells(6, 11) = Evaluate("=SUMPRODUCT((Sheet2!$A$2:$A$10=Sheet1!B6)*(Sheet2!B1=Sheet1!K5)*Sheet2!$B$2:$B$10)")
 
Upvote 0
Try

Cells(6, 11) = Evaluate("=SUMPRODUCT((Sheet2!$A$2:$A$10=Sheet1!B6)*(Sheet2!B1=Sheet1!K5)*Sheet2!$B$2:$B$10)")

Yeah this Evaluate is cool

Thank you very much. But may I know what problem with my code above? I must use this Evaluate?

I got more workbooks to sum so may i learn more beside Evaluate?

Code:
Cells(6, 11) = Application.WorksheetFunction.Sumproduct((C:\Users\Vanda_a\Desktop\Book1.xls.Sheet2.Range("A2:A10") = Range("B6")) * (C:\Users\Vanda_a\Desktop\Book1.xls.Sheet2.Range("B1") = Range("K5")) * C:\Users\Vanda_a\Desktop\Book1.xls.Sheet2.Range("B2:B10"))
 
Last edited:
Upvote 0
There's nothing 'wrong' with the code you tried.

It's just that sumproduct doesn't work in VBA the same way it does when used in a Cell Formula.
It can't process an array of criteria like (Sheet2!$A$2:$A$10=Sheet1!B6)

It's just the way it is with Sumproduct and VBA.
Evaluate seems to be the only way around it.
 
Upvote 0
It's not really related to SUMPRODUCT - it's that you can't create an array in VBA using a line like:
Code:
Sheet2.Range("A2:A10") = Range("B6")
 
Upvote 0

Forum statistics

Threads
1,214,985
Messages
6,122,605
Members
449,089
Latest member
Motoracer88

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