VBA SUMPRODUCT not same as worksheet SUMPRODUCT

Yamezz

Active Member
Joined
Nov 22, 2006
Messages
367
Office Version
  1. 2019
I have Quantities in column Q, Unit Prices in column U and Account Codes in column T

On the worksheet I can successfully sum the total of each Account Code via =SUMPRODUCT(--(T2:T44="200-002"),Q2:Q44,R2:R44), but when I try the same thing in VBA, I get a type mismatch error.
VBA Code:
XeroWETRetail = Application.WorksheetFunction.SumProduct(--(Xero.Sheets("Xero Sales Invoice").Range("T2:T" & XeroRow) = "200-002"), _
 Xero.Sheets("Xero Sales Invoice").Range("Q2:Q" & XeroRow), Xero.Sheets("Xero Sales Invoice").Range("R2:R" & XeroRow))

What am I overlooking?
 
Alex, I am baffled. I confirm there are no errors as per your last post. I have just re-run the code for the umpteenth time and now it executes correctly. Nothing has changed - the input files for the code are exactly unmodified from their original form. I will continue on with my project and see what happens. Thanks for your help.
 
Upvote 0

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Actually, I do have a further query on this. I need to run essentially the same SUMPRODUCT on the source data (as a check to see how different the source data is from the macro-generated data - there will be rounding errors I need to deal with later). The first EVALUATE snippet you gave me refers to a known sheet name in the workbook where the macro is stored, but the second instance needs to refer to SalesReport.Activesheet, a different workbook with a sheet name that will be different each time the file is downloaded. How do I refer to that location in the EVALUATE code?
 
Upvote 0
If you want to run it on the activeworkbook on its active sheet, just don't reference the workbook or sheet and it will default to the activesheet eg
Note: You will need to do the same thing when you calculate the LastRow (in this currently called XeroRow) and you won't be able to refer to ThisWorkbook it would need to be the ActiveWorkbook.
VBA Code:
XeroWETRetail = Evaluate("SUMPRODUCT(--(T2:T" & XeroRow & "=""200-002"")," & _
                             "Q2:Q" & XeroRow & ",R2:R" & XeroRow & ")")
 
Upvote 0
Alex, if I was to step through a list of account codes to retrieve the SUMPRODUCT, how would I use a variable in this line of code? I have tried a few variations, but Excel is not happy with any of them. Something like
VBA Code:
XeroWETRetail = Evaluate("SUMPRODUCT(--(T2:T" & XeroRow & "=" & AccCode")," & "Q2:Q" & XeroRow & ",R2:R" & XeroRow & ")")
 
Upvote 0
Try this:
VBA Code:
XeroWETRetail = Evaluate("SUMPRODUCT(--(T2:T" & XeroRow & "=""" & AccCode & """)," & _
                             "Q2:Q" & XeroRow & ",R2:R" & XeroRow & ")")
 
Upvote 0
Thanks Alex. While implementing this, I received the same old type mismatch error, which only occurred sometimes, depending on where I was looking prior to pressing F8. I've since realised that immediately prior to the problem line of code, the focus of Excel was not on the correct workbook.
Now, onto my next problem! (New thread coming).
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,201
Members
453,022
Latest member
RobertV1609

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