Passing an integer variable into VBA evaluate SumProduct

Mericle

New Member
Joined
Mar 23, 2020
Messages
2
Office Version
  1. 365
Platform
  1. Windows
I'm trying to pass a variable into my evaluate statement and I can't get the syntax correct. Let me know if you can help

This works:
Labor = Evaluate("=sumproduct(--(g10:g50000="Forecast"), --(j10:J50000="CWIP"), --(y10:y50000=2020), --(z10:z50000=4),(aq10:aq50000))")

but I want to change the 4 to be a variable for the month, so I tried this:
Dim Month as Integer
Month = 4
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
How about
VBA Code:
Labor = Evaluate("=sumproduct(--(g10:g50000=""Forecast""), --(j10:J50000=""CWIP""), --(y10:y50000=2020), --(z10:z50000=" & Month & "),(aq10:aq50000))")
 
Upvote 0
omg it was that easy. I feel so dumb. Thank you so much for your help. Sometimes it just takes another set of eyes!
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0
This works:
Labor = Evaluate("=sumproduct(--(g10:g50000="Forecast"), --(j10:J50000="CWIP"), --(y10:y50000=2020), --(z10:z50000=4),(aq10:aq50000))")
but I want to change the 4 to be a variable for the month

At a minimum, you might consider:

Labor = Evaluate("SUMIFS(AQ10:AQ50000, G10:G50000, ""Forecast"", J10:J50000, ""CWIP"", Y10:Y50000, 2020, Z10:Z50000," & Month & ")")

SUMIFS should be much more efficient than SUMPRODUCT.

Note the use of two double-quotes where you need one double-qoute in the string. That correction applies in either case.

Both formulas presume that column Z contains month numbers(!), not dates. Is that right?

Finally, you might consider the following alternative:

Labor = WorksheetFunction.SUMIFS(Range("AQ10:AQ50000"), Range("G10:G50000"), "Forecast", Range("J10:J50000"), "CWIP", Range("Y10:Y50000"), 2020, Range("Z10:Z50000"), Month)

Besides the simpler syntax, it might be more efficient to do because all of the calculation is in the VBA thread. In contrast, Evaluate causes the calculation to be done in the Excel thread.

OTOH, the overhead of the Range references might offset any benefit of WorksheetFunction.SUMIFS v. Evaluate. I have not measured it.
 
Upvote 0

Forum statistics

Threads
1,223,891
Messages
6,175,229
Members
452,621
Latest member
Laura_PinksBTHFT

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