Problem using SUMPRODUCT in VBA

jakuza

New Member
Joined
Jul 9, 2012
Messages
36
I am using UDF function in one of my Sheets.

The part of code is:

Code:
Public Function MyBook(number As Integer, mycell As Range) As Variant

Dim MySum, SumFirst As Integer, SumExtra As String

    For Each WS In Worksheets
        If WS.CodeName = "Sheet" & number Then
            MyName = WS.Name
            Exit For
        End If
    Next WS

[B]MySum = Evaluate("=SUMPRODUCT(--('" & Sheets(MyName).Name & "'!D1:D1000=""Black""" & ");--('" & Sheets(MyName).Name & "'!J1:J1000<0);'" & Sheets(MyName).Name & "'!J1:J1000)")[/B]

SumFirst = WorksheetFunction.SumIf(Sheets(MyName).Range("D1:D1000"), ActiveSheet.Range(mycell.Address).Value, Sheets(MyName).Range("J1:J1000"))

SumExtra = " (" & MySum & ")"
MyBook = SumFirst & SumExtra

End Function

But above code give #VALUE! as a result in the cell. Can someone figure out why? I used MsgBox to test if the string in Evaluate function is good, and it seems that it is exactly as the one in worksheet that is working well. So, there must be something wrong with Evaluate function. Also, I tried with "*" instead of "--" in SUMPRODUCT but no luck.

Any help?
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Try replacing the semicolons in the formula with commas.
 
Upvote 0
Glad to help. :)

(VBA believes that only America exists and therefore defaults to US locale settings for almost everything unless you specify otherwise, where you can)
 
Upvote 0

Forum statistics

Threads
1,207,094
Messages
6,076,549
Members
446,212
Latest member
KJAYPAL200

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