I want to use following SUMPRODUCT
formula in VBA:
=SUMPRODUCT((Sale!$J$5:$J$1048576=C12)*Sale!$D$5:$D$1048576,Sale!$M$5:$M$1048576)
I had the same problem with sumproduct function and after many experiments I solved my problem with this code:
Sub Test2()
Dim WS As Worksheet
Dim a, b, c, Criteria As Range
Dim data1, data2, data3, crite As String
Dim LasTRow As Long
Set WS = ThisWorkbook.Sheets("Sale")
LasTRow = WS.Cells(Rows.Count, 1).End(xlUp).Row
Set a = WS.Range("A5:A" & LasTRow)
Set b = WS.Range("b5:b" & LasTRow)
Set c = WS.Range("c5:c" & LasTRow)
Set Criteria = WS.Range("A1")
data1 = a.Address
data2 = b.Address
data3 = c.Address
crite = Criteria.Address
WS.Range("b1").Formula = "=sumproduct((" & data1 & " = " & crite & ") *( " & data2 & " ) * ( " & data3 & " ))"
End Sub