Excel VBA工作簿未添加到对象集合

时间:2012-07-31 16:03:05

标签: vba excel-vba automation office-automation excel

我正在尝试创建一组工作簿,这些工作簿将使我能够更轻松地跟踪所有打开的工作簿。因为每次都会以不同的方式命名工作簿,并且因为我需要在不假设Excel没有打开其他工作簿的情况下完成此工作,所以我无法使用索引。因此我决定使用一个系列。但是,当我输入第二个工作簿时,我一直收到错误,而且我不确定是怎么回事。我已经将代码设置为两个模块(我不确定这是否有问题),但我刚刚提供了相关代码以便于阅读。

Sub run()
   Dim usedWorkbooks As New Collection
   Dim mainWorkBook As Workbook
   Set mainWorkBook = ActiveWorkbook
   Dim testwb As Workbook

   usedWorkbooks.Add Item:=mainWorkBook, key:="main" 'Added successfully
   usedWorkbooks.Add Item:=testwb, key:="test" 'Added successsfully
   addNewFile(usedWorkBooks)
End Sub

'In a separate module
Public Sub addNewFile(ByRef usedWorkBooks as Collection) 
   Dim ptCsv As String
   ptCsv = someFilePath
   'Filegrabber.grab simply returns the path of the desired workbook and works correctly
   'This is not the problem, can be substituted with any file path string
   ptCsv = FileGrabber.grab(ptCsv) 
   Dim ptCsvWorkBook As Workbook
   Set ptCsvWorkBook = Workbooks.Open(ptCsv) 'Successfully opens workbook

   'Prints out the type of object as "Workbook"
   MsgBox "the object is: " & TypeName(ptCsvWorkBook)
   'Fails to add. Says I need an object  
   usedWorkbooks.Add Item:=ptCsvWorkBook, key:="ptCsv"
End Sub 

我不确定是否是因为该集合位于不同的模块中,或者上面的代码有问题。我完全失去了。任何帮助都会很棒。

更新

我已经回答了这个问题,但是在相关的说明中,似乎每次我引用该集合时,它都会消除所有以前的集合部分。我在下面将这段代码放在我的答案中,但我在这里修改它以供参考:

'UserForm to login to system
Dim usedWorkbooks As New Collection 
Private Sub login_Click()
   Dim mainWorkBook As Workbook
   Set mainWorkBook = ActiveWorkbook

   usedWorkbooks.Add Item:=mainWorkBook, key:="main"
   usedWorkBooks.Add Item:=testwb, key:="test"
   MsgBox "the size of the array is: " & usedWorkBooks.Count 'Prints out 2 as the size

   intializeProcess 'a worksheet is added to the array here
   'This will print out as saying there are 0 worksheets
   MsgBox "the size of the array is: " & usedWorkBooks.Count
End Sub

Public Sub addNewFile(filepath As String, sheetKey As String)
   Dim newWorkBook As Workbook

   Set newWorkBook = Workbooks.Open(filepath)

   usedWorkBooks.Add Item:=newWorkBook, key:=sheetKey
   MsgBox "the size of the array is: " & usedWorkBooks.Count

End Sub

'Part of the initialize Module
Public Sub intializeProcess()
   Call LoginModule.login(username, password, "pt", ie)
   ptCsv = FileGrabber.grab(ptCsv) 'ptcsv is set to some filepath by the grab sub

   'This will print out that the array size is 1
   UserLogin.addNewFile ptCsv, "ptCsv"  
End Sub

我不明白发生了什么。好像你无法在VBA中传递集合的引用?我已经阅读过有关New操作实际上没有创建对象的帖子,但这对我来说并不清楚。如果你认为这应该是一个单独的问题让我知道,我会这样做。感谢。

1 个答案:

答案 0 :(得分:0)

上面的代码被重写,以防止在页面上有太多代码。我不确定我修改了哪些更改,所以我添加了之前的原始代码,以防有人想要解决原始问题。我应该在将来遇到问题时发布整个代码:

'UserForm to login to system
Dim usedWorkbooks As New Collection 'Correct placement of collection
Private Sub login_Click()
   Dim usedWorkbooks As New Collection 'Should be defined outside of this sub
   Dim mainWorkBook As Workbook
   Set mainWorkBook = ActiveWorkbook

   usedWorkbooks.Add Item:=mainWorkBook, key:="main"
   intializeProcess
End Sub

Public Sub addToWorkbookCollection(obj As Workbook, name As String)
   usedWorkBooks.Add Item:=obj, key:=name
End Sub

'Part of the initialize Module
Public Sub intializeProcess()
   ptCsv = FileGrabber.grab(ptCsv)
   Dim ptCsvWorkBook As Workbook

   Set ptCsvWorkBook = Workbooks.Open(ptCsv)

   MsgBox "the object is: " & TypeName(ptCsvWorkBook)
   UserLogin.addToWorkbookCollection ptCsvWorkBook, "ptCsv"

End Sub

在阅读上面刚刚输入的代码后,我意识到我在login_click()子区域内创建了集合,从而产生了范围问题。对这个问题感到抱歉,但至少它有助于解决我的问题。