Evaluate Sumproduct syntax

cortexnotion

Board Regular
Joined
Jan 22, 2020
Messages
150
Office Version
  1. 2013
Platform
  1. Windows
Hi All

Can you help me with the sumproduct formula in my code - it is returning a VALUE entry in the cell atm.

Code:
Sub check()

Application.ScreenUpdating = False

Dim wbtracker As Workbook
Dim WeekRng As Range, TypeRng As Range, TotRng As Range
Dim Chart1 As String, Chart2 As String
Dim WK1 As Date, WK2 As Date, WK3 As Date, WK4 As Date, WK5 As Date, WK6 As Date, LRhh As Long

Set wbtracker = Workbooks.Open("S:\temp\Tracker.xlsx")
LRhh = wbtracker.Sheets("HH").Range("A" & Rows.Count).End(xlUp).Row

Set WeekRng = wbtracker.Sheets("HH").Range("A2:A" & LRhh)
Set TypeRng = wbtracker.Sheets("HH").Range("B2:B" & LRhh)
Set TotRng = wbtracker.Sheets("HH").Range("W2:W" & LRhh)

Chart1 = "Raw"
Chart2 = "Good"

WK1 = Application.Evaluate("INT((TODAY()-1)/7)*7+1")
WK2 = Format(WK1 - 7, "dd/mm/yyyy")
WK3 = Format(WK2 - 7, "dd/mm/yyyy")
WK4 = Format(WK3 - 7, "dd/mm/yyyy")
WK5 = Format(WK4 - 7, "dd/mm/yyyy")
WK6 = Format(WK5 - 7, "dd/mm/yyyy")

With wbtracker.Sheets("Work")
.Range("A2").Value = Application.Evaluate("(SumProduct((WeekRng = WK1)) * (TypeRng = Chart1) * (TotRng))")
.Range("A3").Value = Application.Evaluate("(SumProduct((WeekRng = WK2)) * (TypeRng = Chart1) * (TotRng))")
.Range("A4").Value = Application.Evaluate("(SumProduct((WeekRng = WK3)) * (TypeRng = Chart1) * (TotRng))")
.Range("A5").Value = Application.Evaluate("(SumProduct((WeekRng = WK4)) * (TypeRng = Chart1) * (TotRng))")
.Range("A6").Value = Application.Evaluate("(SumProduct((WeekRng = WK5)) * (TypeRng = Chart1) * (TotRng))")
 .Range("A7").Value = Application.Evaluate("(SumProduct((WeekRng = WK6)) * (TypeRng = Chart1) * (TotRng))")
End With

End Sub

Many thanks
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
You need to pass a formula string to Evaluate as you would use it in a cell. You can't use VBA variables in a formula, so you should be using the address of the ranges and the literal comparison values in the formula string.
 
Upvote 0
Thanks for the reply - the range and criteria are dynamic.

Is there an alternative way to pass the formula through with VBA variables?
Thanks
 
Upvote 0
Make the range variables into String:

Code:
WeekRng = "'HH'!A2:A" & LRhh
TypeRng = "'HH'!B2:B" & LRhh
TotRng = "'HH'!W2:W" & LRhh

then:

Code:
.Range("A2").Value = Application.Evaluate("(SumProduct((" & WeekRng & "=" & WK1 & ")*(" & TypeRng & "=""" & Chart1 & """)," & TotRng & ")")

etc
 
Upvote 0
Thank you, I get what you mean now. I still get a VALUE error but is this because the code is not run from the activeworkbook. I am running the code from my first workbook and opening the second workbook to do this calculations and then closing again

Do I need to reference the workbook name in the 'HH' part? It is a fixed workbook name if that helps?
 
Upvote 0
Try using wbtracker.Sheets("HH").Evaluate instead of application.evaluate.
 
Upvote 0
Unfortunately still getting the VALUE error and my ranges are definitely correct. Here is what I have now:

Code:
]
Sub check()

Application.ScreenUpdating = False

Dim wbtracker As Workbook
Dim WeekRng As String, TypeRng As String, TotRng As String
Dim Chart1 As String, Chart2 As String
Dim WK1 As String, WK2 As Date, WK3 As Date, WK4 As Date, WK5 As Date, WK6 As Date, LRhh As Long

Set wbtracker = Workbooks.Open("S:\temp\Tracker.xlsx")
LRhh = wbtracker.Sheets("HH").Range("A" & Rows.Count).End(xlUp).Row

WeekRng = "'HH'!A2:A" & LRhh
TypeRng = "'HH'!B2:B" & LRhh
TotRng = "'HH'!C2:C" & LRhh

Chart1 = "Raw"
Chart2 = "Good"

WK1 = Application.Evaluate("INT((TODAY()-1)/7)*7+1")
WK2 = Format(WK1 - 7, "dd/mm/yyyy")
WK3 = Format(WK2 - 7, "dd/mm/yyyy")
WK4 = Format(WK3 - 7, "dd/mm/yyyy")
WK5 = Format(WK4 - 7, "dd/mm/yyyy")
WK6 = Format(WK5 - 7, "dd/mm/yyyy")

MsgBox WK1

With wbtracker.Sheets("Work")
.Range("A2").Value = wbtracker.Sheets("HH").Evaluate("(SumProduct((" & WeekRng & "=" & WK2 & ")*(" & TypeRng & "=""" & Chart1 & """)," & TotRng & ")")
End With
 
End Sub {/code}
 
Upvote 0
Just spotted we had a ")" missing off the formula and now working well!

Thanks for your help today @RoryA
 
Upvote 0

Forum statistics

Threads
1,214,651
Messages
6,120,744
Members
448,989
Latest member
mariah3

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