我是收藏和字典的新手。
我能够使用集合来填充数据,现在我需要帮助来了解在满足项目(字段)时如何求和。
'SAPBWData Class Module
Public ProfitCenter As String
Public PLCode As String
Public GL As String
Public Year3Total As Double
'Module1
Public DataCollection As New Collection
Sub PopulateData()
Dim sapbw As SAPBWData
Dim r As Range
Dim sh As Worksheet
Set sh = ThisWorkbook.Worksheets("Sheet1")
For Each r In sh.Range("A2:A6")
Set sapbw = New SAPBWData
sapbw.ProfitCenter = CStr(r.Value)
sapbw.PLCode = r.Offset(0, 1).Value
sapbw.GL = r.Offset(0, 2).Value
sapbw.Year3Total = r.Offset(0, 3).Value
DataCollection.Add sapbw
Set sapbw = Nothing
Next r
End Sub
Function vbaSUMIFS(ProfitCenter_Lookup As Range, PLCode_Lookup As Range, Optional GL_Lookup As Range) As Variant
Dim myKey As Variant
If GL_Lookup.Value <> "" Then
myKey = ProfitCenter_Lookup.Value & ";" & PLCode_Lookup.Value & ";" & GL_Lookup.Value
Else
myKey = ProfitCenter_Lookup.Value & ";" & PLCode_Lookup.Value
End If
'Need to know how to SUM the values based on the items that user select.
End Function
我这里想要的基本上是,如果用户选择2个项目(Profit Center和PLCode),我希望集合基于这些项目进行汇总。但是,如果选择了第三个项目(GL),那么我想基于这三个项目进行总结。
基本上,使用SUMIFS函数类似于。但是我不能使用它,因为计算非常慢。因此必须对VBA集合/词典有所贡献。
注意:我已经尝试使用GETPIVOTDATA,SUMPRODUCT等,并且计算无法承受。