gpt4 book ai didi

linq - 汇总 VBA 集合中的记录

转载 作者:行者123 更新时间:2023-12-04 20:57:05 29 4
gpt4 key购买 nike

我有一个由 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

最佳答案

您所希望的可以通过 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

即时窗口中的结果是 626 .一般来说,将集合转换为数组并不难。

关于linq - 汇总 VBA 集合中的记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45285178/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com