我有一個子創建一個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
也會返回一個集合。我嘗試添加/刪除括號,但無濟於事。
您的代碼是不完整的,所以你會發現幫助較少即將出現。 Sub storeBudgetWorkbooks缺失,什麼是所有的BudgetsItems變暗,因爲是顯而易見的問題... – Tragamor
你是否已經使用f8(或將VBE設置爲在課程模塊中斷)進入課程代碼以找到實際問題行? – Rory
@Rory,你是什麼意思?我不知道如何找出實際問題,它只是強調函數調用的行,而不是函數內的特定行。 – Ans