总结记录

问题描述:

我有一个VBA集合组成DayTots类对象(见下文)总结记录

我使用的是对于每个迭代直通集合创建 新的集合组成的总结记录的基础上,日期

有没有什么办法可以和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

Linq在VBA/Office中不存在,所以,不行。 –

什么,你都希望能与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。一般来说,将收集转换为数组并不困难。

+0

谢谢。这让我更加接近。但是有没有办法将求和限制在与键匹配的条目上? –