我有一个由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
答案 0 :(得分:0)
您希望通过Array
和WorksheetFunction.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
即时窗口中的结果是6
和26
。通常,将集合转换为数组并不困难。