Evaluate Sumproduct syntax

cortexnotion

Board Regular
Joined
Jan 22, 2020
Messages
138
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

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
36,696
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
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.
 

cortexnotion

Board Regular
Joined
Jan 22, 2020
Messages
138
Office Version
  1. 2013
Platform
  1. Windows
Thanks for the reply - the range and criteria are dynamic.

Is there an alternative way to pass the formula through with VBA variables?
Thanks
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
36,696
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
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
 

cortexnotion

Board Regular
Joined
Jan 22, 2020
Messages
138
Office Version
  1. 2013
Platform
  1. Windows

ADVERTISEMENT

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?
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
36,696
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
Try using wbtracker.Sheets("HH").Evaluate instead of application.evaluate.
 

cortexnotion

Board Regular
Joined
Jan 22, 2020
Messages
138
Office Version
  1. 2013
Platform
  1. Windows
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}
 

cortexnotion

Board Regular
Joined
Jan 22, 2020
Messages
138
Office Version
  1. 2013
Platform
  1. Windows
Just spotted we had a ")" missing off the formula and now working well!

Thanks for your help today @RoryA
 

Watch MrExcel Video

Forum statistics

Threads
1,129,486
Messages
5,636,614
Members
416,929
Latest member
Nitil

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
Top