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.
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
What is the active sheet when the sub reaches the Evaluate code line?

For testing purposes, assuming data in Sheet1 (adjust the sheet name to suit), see if this works

Code:
prop.Offset(0, 2).Value = Application.Evaluate("=SumProduct(([COLOR=#ff0000]Sheet1![/COLOR]" & _
      Range(myUsedRange.Offset(0, 3), myUsedRange.Offset(0, 5)).Address & "=" _
      & Chr(34) & prop.Value & Chr(34) & ")*([COLOR=#ff0000]Sheet1![/COLOR]" & myUsedRange.Offset(0, 6).Address & "))")

Hope this helps

M.
 
Upvote 0
Thanks for the reply Marcelo.

I have added the sheet name and when stepping through, make sure the sheet is selected.
It still comes out as #value
I put prop.Offset(0, 3).formula = "=SumProduct ... etc
But this comes out correct with the formula in the cell - so why will it not evaluate from vba??
 
Upvote 0
George

It works for me with some simple data.

Are there any error values in the data involved in the formula?
 
Upvote 0
Does this work:

Code:
prop.Offset(0, 2).Value = myUsedRange.Worksheet.Evaluate("=SumProduct((" & _
      Range(myUsedRange.Offset(0, 3), myUsedRange.Offset(0, 5)).Address & "=" _
      & Chr(34) & prop.Value & Chr(34) & ")*(" & myUsedRange.Offset(0, 6).Address & "))")
 
Upvote 0
Check that the formula is being constructed as you think it is..

Run it this way, and look in the immediate window for the resulting text string.
Code:
Debug.Print "=SumProduct((Sheet1!" & _
      Range(myUsedRange.Offset(0, 3), myUsedRange.Offset(0, 5)).Address & "=" _
      & Chr(34) & prop.Value & Chr(34) & ")*(Sheet1!" & myUsedRange.Offset(0, 6).Address & "))"
 
Upvote 0
Thanks for the replies guys.

I put this into a smaller workbook with test code to make sure there was not something I was missing with the amount of data. It still comes out the same processing 9 rows. Not sure if this is a formatting issue, but there are not error values within the data and no error messages come up. I got the error 2015 when using the immediate window to see what was happening.

I tried RoryA's suggestion, but the result is the same.

Here is a link to my example workbook. (Data on sheet 2)
https://drive.google.com/file/d/0B1MotIND4aIDcFY2STBGdmJkZGc/view?usp=sharing
 
Upvote 0
This may just be a regional thing, but when I open that book the values in G2:G10 are TEXT strings, not numbers.
TEXT*Nmber = #Value!

It appears to be the £ symbol.
If I remove those £ symbols, then your code works fine.
 
Upvote 0
They are also Numbers stored as Text for me and I'm in the same region.
 
Upvote 0
The issue is that the formula version performs implicit coercion, but it doesn't do that within Evaluate. You can fix it by using:
Code:
        sFormula = "=SumProduct((" & Range(myUsedRange.Offset(0, 3), myUsedRange.Offset(0, 5)).Address & "=" _
       & prop.Address & ")*(SUBSTITUTE(" & myUsedRange.Offset(0, 6).Address & ",""£"","""")))"
    prop.Offset(0, 2).Value = myUsedRange.Worksheet.Evaluate(sFormula)
 
Upvote 0

Forum statistics

Threads
1,217,394
Messages
6,136,341
Members
450,005
Latest member
BigPaws

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