0

I am trying to create a collection of workbooks that will allow me to more easily keep track of all the workbooks that are opened. Because the workbooks will be named differently each time, and because I need to make this work without assuming that Excel has no other workbooks open, I cannot use the index. Therefore I have decided to use a collection. However, I keep getting an error when i put in a second workbook, and I am not sure what is going on. I have set the code up in two modules (I am not sure if that would be a problem), but i have just provided the relevant code for easy reading.

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 

I am not sure if it is because the collection is in a different module, or if there is something wrong with my code above. I am at a total loss. Any help would be great.

UPDATE

I have since answered this question, but on a related note, it seems that each time i reference the collection it is wiping out all of the previous parts of the Collection. I placed this code below in my answer, but i am adding it here modified for reference:

'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

I don't understand what is going on. It seems like you are unable to pass the reference of collections in VBA? I have read posts about how the New operation doesn't actually create an object, but that was unclear to me. If you think this should be a separate question let me know and i will make it so. Thanks.

4

1 回答 1

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() 子中创建了集合,从而产生了范围问题。对不起这个问题,但至少它帮助解决了我的问题。

于 2012-07-31T17:31:26.787 回答