SUMPRODUCT Formula in VBA

前端 未结 3 826
粉色の甜心
粉色の甜心 2021-01-14 10:07

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)
         


        
3条回答
  •  -上瘾入骨i
    2021-01-14 10:36

    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
    

提交回复
热议问题