对 VBA 集合中的记录求和



>我有一个由DayTots类对象组成的VBA集合(见下文(

我正在使用 For Each 来迭代集合以创建一个 由基于日期的汇总记录组成的新集合

有什么方法可以用 Linq 做到这一点吗? 我怀疑也许我可以替换 几行代码,内容更简单、更直接 使用 Linq 查询(也许使用组?

我在互联网上找不到任何似乎可以解决这种情况的内容

提前感谢所有帮助

输入收集

tDate       TotTrav TotTrav TotParts
Yes     No      Accepted
2/27/2017   1       0       1
2/27/2017   1       0       0
2/27/2017   1       0       0
2/27/2017   0       1       0
2/28/2017   1       0       1
2/28/2017   0       1       0 
2/28/2017   0       0       1

输出集合

DatDate     TotTrav TotTrav TotParts
Yes     No      Accepted
2/27/2017   3       1       1
2/28/2017   1       1       2
Dim cumRecs As DayTots   
dim incoll, outcoll as Collection
outcoll = New Collection
For Each irec In incoll
....
outcoll.Add(cumRecs)
Next irec
'Class DayTots
Dim DatDate              As Date
Dim TravYes              As Integer
Dim TravNo               As Integer
Dim PartsAccepted        As Integer
Public Property Get tDayDate() As Date
tDayDate = DatDate
End Property
Public Property Let tDayDate(value As Date)
DatDate = value
Weekno = Int(((DatDate - DateSerial(Year(DatDate), 1, 0)) + 6) / 7)
End Property
Public Property Get TotTravYes() As Integer
TotTravYes = TravYes
End Property
Public Property Let TotTravYes(value As Integer)
TravYes = value
End Property
Public Property Get TotTravNo() As Integer
TotTravNo = TravNo
End Property
Public Property Let TotTravNo(value As Integer)
TravNo = value
End Property
Public Property Get TotPartsAccepted() As Integer
TotPartsAccepted = PartsAccepted
End Property
Public Property Let TotPartsAccepted(value As Integer)
PartsAccepted = value
End Property

你所希望的可以通过ArrayWorksheetFunction.Sum来实现 下面是一个小例子:

Option Explicit
Public Sub TestMe()
Dim inColl  As New Collection
Dim inArr   As Variant
inColl.Add 1
inColl.Add 2
inColl.Add 3
Debug.Print WorksheetFunction.Sum(inColl.Item(1), inColl.Item(2), inColl.Item(3))
inArr = Array(5, 6, 7, 8)
Debug.Print WorksheetFunction.Sum(inArr)
End Sub

即时窗口中的结果是626。通常,将集合转换为数组并不困难。

最新更新