Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 集合.Add:参数数目错误或属性分配无效_Vba_Excel - Fatal编程技术网

Vba 集合.Add:参数数目错误或属性分配无效

Vba 集合.Add:参数数目错误或属性分配无效,vba,excel,Vba,Excel,我有一个sub,它创建一个集合,并在其中添加集合s。但是,在循环中添加第一个集合时,我得到了一个错误的参数数或无效的属性分配错误: Sub testState() Dim stateCopy As State Set stateCopy = New State stateCopy.setStateName="some name" stateCopy.storeBudgetWorkbooks stateCopy.storeBudgetDatas 'erro

我有一个sub,它创建一个
集合
,并在其中添加
集合
s。但是,在循环中添加第一个集合时,我得到了一个
错误的参数数或无效的属性分配
错误:

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

我不确定这里出了什么问题
getBudgetItems
返回一个集合
getBudgetData
还返回一个集合。我试着添加/删除括号,但没有成功。

找到了答案。应该有
设置getBudgetData=budgetItems(年)
,而不是
getBudgetData=budgetItems(年)

解决了这个问题。应该有
Set getBudgetData=budgetItems(year)
而不是
getBudgetData=budgetItems(year)

因为您没有向我们展示代码的所有相关部分,所以我只能猜测您缺少一个
集合:

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

由于您尚未向我们展示代码的所有相关部分,我只能猜测您缺少一个

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

您的代码不完整,因此您可能会发现帮助不太方便。Sub Store BudgetWorkBooks丢失,所有BudgetItems都变暗,因为存在明显的问题…您是否使用f8(或将VBE设置为打断类模块)进入类代码以查找实际的问题行?@Rory,这是什么意思?我不知道如何找出实际的问题,比如,它只是突出显示函数调用的行,而不是函数内部的特定行。如果在按Debug之后按f8,调试器是否会进入类代码?大概
budgetWorkbooks
是类中的另一个函数?如果是这样,在调用
getBudgetData
之前,您是否可以检查
budgetWorkbook
是否已设置为工作簿?您的代码不完整,因此您可能会发现不太容易获得帮助。Sub Store BudgetWorkBooks丢失,所有BudgetItems都变暗,因为存在明显的问题…您是否使用f8(或将VBE设置为打断类模块)进入类代码以查找实际的问题行?@Rory,这是什么意思?我不知道如何找出实际的问题,比如,它只是突出显示函数调用的行,而不是函数内部的特定行。如果在按Debug之后按f8,调试器是否会进入类代码?大概
budgetWorkbooks
是类中的另一个函数?如果是,在调用
getBudgetData
之前,是否可以检查
budgetWorkbook
是否已设置为工作簿?