类型不匹配,将工作表分配给数组

时间:2018-06-27 14:53:02

标签: vba excel-vba excel

快速问题:我有一个函数,它接受工作簿数组和要查找的工作表的名称。尝试将每个工作表添加到数组时出现类型不匹配的情况。功能如下:

Function AssignSheets(books() As Object, name As String) As Object()
Dim worksheetarray() As Object
ReDim workbookarray(1 To FNum)

For i = LBound(books) To UBound(books)
    Set worksheetarray(i) = workbooks(books(i)).Worksheets(name)
Next i

AssignSheets = worksheetarray

End Function

Workbooks(books(i))。Worksheets(name)是否应该返回对象?任何见解将不胜感激,谢谢!

1 个答案:

答案 0 :(得分:0)

感谢吉普的评论,他确实是回答我问题的人!

第一个错误:ReDim workbookarray(1到FNum)应该是ReDim worksheetarray(1到FNum)

第二个错误:正如吉普(Jeeped)所说,我引用:

“如果将书籍设置为工作簿,则应将其设置为Worksheetarray(i)= books(i).Worksheets(name)。您将书籍的元素视为工作簿名称,而不是工作簿对象”

这是更正的代码:

Function AssignSheets(books() As Object, name As String) As Object()
Dim worksheetarray() As Object
ReDim worksheetarray(1 To FNum)

For i = LBound(books) To UBound(books)
    Set worksheetarray(i) = books(i).Worksheets(name)
Next i

AssignSheets = worksheetarray

End Function

感谢大家的快速回复!