答案 0 :(得分:0)
字典会构建一个快速的SUMIF并在此过程中构建唯一的零售商列表。
Sub mySumIf()
Dim i As Long, arr As Variant, retailer As string, dict As Object
Set dict = CreateObject("scripting.dictionary")
dict.comparemode = vbTextCompare
With Worksheets("tab 1")
'collect source values into array
arr = .Range(.Cells(2, "A"), .Cells(.Rows.Count, "C").End(xlUp)).Value2
End With
retailer = UCase(Worksheets("tab 2").Cells(4, "F").Value2)
'build a dictionary with Stores as keys and Sales as Items
For i = LBound(arr, 1) To UBound(arr, 1)
If UCase(arr(i, 2)) = retailer Then
dict.iktem(arr(i, 1)) = dict.iktem(arr(i, 1)) + arr(i, 3)
End If
Next i
With Worksheets("tab 2")
'put Stores and Summed Sales onto target worksheet
.Cells(6, "E").Resize(dict.Count, 1) = Application.Transpose(dict.keys)
.Cells(6, "F").Resize(dict.Count, 1) = Application.Transpose(dict.items)
End With
End Sub
答案 1 :(得分:0)