2017-07-24 95 views
1

我有一個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中不存在,所以,不行。 –

回答

0

什麼,你都希望能與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

謝謝。這讓我更加接近。但是有沒有辦法將求和限制在與鍵匹配的條目上? –