Using Sumproduct in VBA

George J

Well-known Member
Joined
Feb 15, 2002
Messages
959
I am struggling with this - i think my text string is correct and when copying it to a cell to test, works okay, but I get error 2015 in VBA. I can't use sumif as the numbers are entered as text on the workbook.

Code:
prop.Offset(0, 2).Value = Application.Evaluate("=SumProduct((" & _
      Range(myUsedRange.Offset(0, 3), myUsedRange.Offset(0, 5)).Address & "=" _
      & Chr(34) & prop.Value & Chr(34) & ")*(" & myUsedRange.Offset(0, 6).Address & "))")
This is the code I have which returns #value on the workbook. When entering the text to be evaluated as a formula however: =SumProduct(($D$2:$F$2083="my text")*($G$2:$G$2083))
It seems to work fine, so not sure why an error is coming up.

Can anyone point me in the right direction?
thanks.
 
The issue is that the formula version performs implicit coercion, but it doesn't do that within Evaluate.
Yes it does.
It's using the sumproduct function by itself in VBA that doesn't do the coercion

This doesn't work
x = Application.Sumproduct((range=criteria),ValueToSum)

This does
x = Evaluate("=SUMPRODUCT((range=criteria),ValueToSum)")
 
Last edited:
Upvote 0
Thanks for the help guys. Not sure I would ever have got there on my own.
I never thought about the £ making a difference in VBA as it worked in the formula.

Really appreciate it guys. :beerchug:
 
Upvote 0
You're welcome.

I don't see how it could have worked in a formula either.

Can you post a formula that worked in the same book that you posted ?
 
Upvote 0
FYI, I was using SUMPRODUCT within Evaluate.

The issue is, as I stated, that the conversion to numeric does not happen within the formula if you are passing the formula string to Evaluate. This appears to be due to the * operator failing when multiplying the strings. (you can't use the version of SumProduct you posted here because the criteria is a three column array, so you have to multiply)
 
Upvote 0
It should be in column Q
I was sending the formula from VBA to check that it was coming out as I thought and the formula seems to work.
 
Upvote 0
FYI, I was using SUMPRODUCT within Evaluate.

The issue is, as I stated, that the conversion to numeric does not happen within the formula if you are passing the formula string to Evaluate. This appears to be due to the * operator failing when multiplying the strings. (you can't use the version of SumProduct you posted here because the criteria is a three column array, so you have to multiply)

Seems to work Just fine. Even with the (multi column array)*(single column array)
 
Upvote 0
I see why the formula worked with £ in the formula, but the VBA evaluate did not..

VBA tends to be US centric, regardless of your Regional Settings.
It would seem that VBA doesn't recognize £ as a currency sympol, therefor the Text values could not be coerced into numbers.
But the FORMULA version DOES recognize £ as a currency symbol.
 
Last edited:
Upvote 0
It would seem that VBA doesn't recognize £ as a currency sympol, therefor the Text values could not be coerced into numbers.
But the FORMULA version DOES recognize £ as a currency symbol.

Confirmed,
I changed my regional settings to use £ as currency symbol
I get the same result you did, formula works but VBA evaluate does not.

Change it back to $ as currency symbol, then neither formula or VBA evaluate works, both return #value.
Until I remove the £ from column G, then both work.
 
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