Problem coding SumProduct in VBA 2003

technowerk

New Member
Joined
Oct 30, 2012
Messages
5
Hi,

Following is a sample data I have. I have written a macro that does various calculations for me. However, now I'm stuck on coding SumProduct function.

CompanyEmployeeEmp_gradeYearSalary
1A320002500
1B219974000
1A320022750
1C219985500
1D319981500
1A320033000
2A320043500
2E120006000
3A220053750
2A220054500
2F220014000
2D220016000
2G120017500
3A120085500
3B320002500
3F319901500
3H220045000
3I120107000
3J320003000
4F120085500
4K120096000
4A120095750
1A120106000
2A120117500

<tbody>
</tbody>

This data is filtered data (I have applied advanced filter to get this data). Now I would like to sum 'Salary' when Employee=A AND Emp_grade=3.

As SUMIF works ONLY for 1 condition, I had to opt for SumProduct.

Here's what I've coded

Code:
Dim TOT_SUM As Double

TOT_SUM=Application.WorksheetFunctions.SumProduct((B2:B65535="A"),(C2:C65535=3),E2:E65535)

This gives an error. I tried recording a macro and using the same code in my code but it doesn't work either. Can someone help please?

I do not want to use pivot table as the actual data is too large to fit on a single sheet of pivot table (about 95 columns and over 12000 rows) and thus there's a risk of losing data if I create a pivot table.

Is there any other function that I can use instead of SumProduct to combine 2 or more conditions in the same statement?

Thanks in advance!
 
Last edited:

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Try:
Code:
TOT_SUM = _
    Application.WorksheetFunction.SumProduct((Range("B2:B65535") = _
    "A") * (Range("C2:C65535") = 3), Range("E2:E65535"))
 
Upvote 0
Try this.
Code:
TOT_SUM = Evaluate("SUMPRODUCT(--(B2:B65535=""A""), --(C2:C65535=3), E2:E65535)")
 
Upvote 0
I do not want to use pivot table as the actual data is too large to fit on a single sheet of pivot table (about 95 columns and over 12000 rows) and thus there's a risk of losing data if I create a pivot table.



If your columns are condensed, you can use pivottable.

Before making a pivot table, you can make an table of your data.

Excel 2007 => Insert => table
 
Upvote 0
Another variant with last row as variable.

Code:
Sub Test()
    Dim s As String
    Dim LR As Long
    Dim TOT_SUM As Double
    
    LR = Cells(Rows.Count, "A").End(xlUp).Row
    s = "SUMPRODUCT(--($B$2:$B$" & LR & "=" & Chr(34) & "A" & Chr(34) & ")," & "--(" & "C2:C" & LR & "=3" & ")," & "E2:E" & LR & ")"
    Debug.Print s
    TOT_SUM = Application.Evaluate(s)
End Sub

Biz
 
Upvote 0
Thank you all for all the valuable inputs! I tried this today and Norie's method seems to work. Can someone please explain what 'Evaluate' exactly does?
 
Upvote 0
Thank you all for all the valuable inputs! I tried this today and Norie's method seems to work. Can someone please explain what 'Evaluate' exactly does?

Evaluate Method converts a Microsoft Excel name to an object or a value
 
Upvote 0
In what I can see it looks like you are missing a parentheses at the end but I suspect it just didn't get copied when you posted this.
You might be able to try separating your conditions with "*" instead of "," that sometimes makes Sumproduct work better in Excel 03 at least. Or you could just write a loop that would do it.

You can use the AND statement in an If to get multiple conditions

sum=0
For i=2 to 65535
If Cells(i,2).Value="A" AND Cells(i,3).Value=3 Then
sum=sum+Cells(i,5)
End If
Next i

You can certainly modify to fit the data instead of going through all 65535 rows.
 
Last edited:
Upvote 0
In what I can see it looks like you are missing a parentheses at the end but I suspect it just didn't get copied when you posted this.
You might be able to try separating your conditions with "*" instead of "," that sometimes makes Sumproduct work better in Excel 03 at least. Or you could just write a loop that would do it.

You can use the AND statement in an If to get multiple conditions

sum=0
For i=2 to 65535
If Cells(i,2).Value="A" AND Cells(i,3).Value=3 Then
sum=sum+Cells(i,5)
End If
Next i

You can certainly modify to fit the data instead of going through all 65535 rows.

Why loop its slow? My post #5 gets last row and it's not hard coding last row as 65535.

Biz
 
Upvote 0

Forum statistics

Threads
1,215,268
Messages
6,123,969
Members
449,137
Latest member
yeti1016

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