Linq VBA集合中的记录求和
我有一个由DayTots类对象组成的VBA集合(见下文) 我使用For Each遍历集合以创建 基于日期的由汇总记录组成的新集合 有什么办法可以让我和Linq一起做吗?我想也许我可以代替他 几行代码,更简单、更直接 使用Linq查询(可能使用组?) 我在互联网上找不到任何能解决这种情况的东西 提前感谢所有的帮助 输入集合Linq VBA集合中的记录求和,linq,excel,vba,Linq,Excel,Vba,我有一个由DayTots类对象组成的VBA集合(见下文) 我使用For Each遍历集合以创建 基于日期的由汇总记录组成的新集合 有什么办法可以让我和Linq一起做吗?我想也许我可以代替他 几行代码,更简单、更直接 使用Linq查询(可能使用组?) 我在互联网上找不到任何能解决这种情况的东西 提前感谢所有的帮助 输入集合 tDate TotTrav TotTrav TotParts Yes No Accepted 2/27/2017 1
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
输出集合
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
您希望通过
数组
和工作表函数实现。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
。一般来说,将集合转换为数组并不困难。Linq在VBA/Office中不存在,因此,不存在。谢谢。这让我更亲近了。但是有没有一种方法可以将求和限制在与键匹配的条目上?