2017-09-11 78 views
0

我有一個子創建一個Collection並在其中添加Collection s。Collection.Add:錯誤的參數數量或無效的屬性分配

Sub testState() 
    Dim stateCopy As State 
    Set stateCopy = New State 
    stateCopy.setStateName="some name" 
    stateCopy.storeBudgetWorkbooks 
    stateCopy.storeBudgetDatas 'error on this line 
End Sub 

Sub storeBudgetDatas() 'inside a class named State 
... 
    Dim year As Integer 
    Dim i As Integer 
    i = 1 
    For year = 2014 To 2017 
     Set budgetWorkbook = 
      ExcelApp.Application.Workbooks.Open(budgetWorkbooks(i)) 
     MsgBox ("still here") 'this message appears 

     allBudgetItems.Add getBudgetData(budgetWorkbook, year) 'this line is likely to cause problems 


     MsgBox ("and here") 'this message doesn't appear 
     budgetWorkbook.Close 
     i = i + 1 
    Next 
End Sub 

Function getBudgetData(budgetWorkbook As Workbook, year As Integer) 
    ... 
    Dim budgetItems As Collection 
    Set budgetItems = getBudgetItems(year) 
    ... 'setting attributes 
    getBudgetData = budgetItems(year) 
End Function 

Function getBudgetItems(year As Integer) 
    ... 
    Dim resultCollection As Collection 
    Set resultCollection = New Collection 
    Dim itemCopy As Item 
    Dim i As Integer 
    For i = LBound(budgetItemNames) To UBound(budgetItemNames) 
     Set itemCopy = New Item 
     ... 'setting attributes 
     resultCollection.Add itemCopy 
    Next 

    Set getBudgetItems = resultCollection 
End Function 

我不知道什麼是錯在這裏:但是我在循環將第一集合時,得到一個Wrong number of arguments or invalid property assignment錯誤。 getBudgetItems返回一個集合。 getBudgetData也會返回一個集合。我嘗試添加/刪除括號,但無濟於事。

+0

您的代碼是不完整的,所以你會發現幫助較少即將出現。 Sub storeBudgetWorkbooks缺失,什麼是所有的BudgetsItems變暗,因爲是顯而易見的問題... – Tragamor

+0

你是否已經使用f8(或將VBE設置爲在課程模塊中斷)進入課程代碼以找到實際問題行? – Rory

+0

@Rory,你是什麼意思?我不知道如何找出實際問題,它只是強調函數調用的行,而不是函數內的特定行。 – Ans

回答

1

想通了。應該有Set getBudgetData = budgetItems(year)而不是getBudgetData = budgetItems(year)

1

既然你已經不是我們顯示你的代碼的所有相關部分,我能做的最好的就是猜你缺少一個Set

Function getBudgetData(budgetWorkbook As Workbook, year As Integer) 
    ... 
    Dim budgetItems As Collection 
    Set budgetItems = getBudgetItems(year) 
    ... 'setting attributes 
    Set getBudgetData = budgetItems(year) ' Need Set here 
End Function 
相關問題