Excel VBA Workbook not adding to Object Collection

耗尽温柔 提交于 2019-12-08 10:36:19

问题


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.


回答1:


The code above was rewritten to keep from having too much code on the page. I am not sure what changes I made fixed the issue, so I have added the original code I had before here in case someone wants to solve the original problem. I should probably post the whole code whenever i have problems in the future:

'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

And after reading the code I just entered above, I realize that I create the collection inside the login_click() sub, thus creating a scope issue. Sorry about the question, but at least it helped solve my issue.



来源:https://stackoverflow.com/questions/11744344/excel-vba-workbook-not-adding-to-object-collection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!