Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8322563
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T23:25:58+00:00 2026-06-08T23:25:58+00:00

This is really stumping me. I put a question up yesterday regarding collections being

  • 0

This is really stumping me. I put a question up yesterday regarding collections being passed between modules (see here), but it doesn’t seem like I am getting anymore explanations on that one, so i am attempting to restate the problem more clearly in a generic way.

I have a module (module1) and a userform (userform1). I create a collection (or array) in userform1 and add worksheet objects to this array. I then pass control to module1, which calls a sub in userform1 called addNewFile, which is supposed to add the newly created workbook to the collection. However, each time module1 calls addNewFile i get one of two scenarios: 1) the collection has been erased and all worksheets that had been added are now gone (for a collection), 2) i get an error saying that i have a type mismatch (for an array). I don’t know why this is happening, so here is the code below to illustrate better. Any help would be appreciated, even if it is just to tell me that it is not possible to store worksheet objects in arrays.

UserForm1

Dim workBooksCollection as New Collection 'can also define as an array
Private Sub CommandButton1_click()

   Dim mainWorkBook as workbook
   Set mainWorkBook = ActiveWorkbook
   Dim testwb As Workbook

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

   Module1.initialize

   'After running initialize, prints off as size 0, meaning collection has been erased
   MsgBox "the size of the array is: " & usedWorkBooks.Count 'Prints off as size 0

End Sub
Public Sub addNewFile(filepath As String, sheetKey As String)
   Dim newWorkBook As Workbook  
   Set newWorkBook = Workbooks.Open(filepath)

   MsgBox "The name of the workbook is: " & newWorkBook.name  'Prints off name of workbook successfully

   workBooksCollection.Add Item:=newWorkBook, key:=sheetKey
   MsgBox "the size of the array is: " & workBooksCollection.Count 'Prints off as size 1
End Sub

Module1

Public Sub intialize()
   Dim filepath as string
   'The filepath is set to any path of a workbook 

   'This will print out that the array size is 1
   UserForm1.addNewFile filePath, "secondBook"

End Sub

Sorry if i seem to be beating a dead horse here, but I really don’t have any idea what is going on here. I am used to the idea of collections and lists being global and not changing when being referenced by another module. Any help on what is going on here would be great.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T23:26:00+00:00Added an answer on June 8, 2026 at 11:26 pm

    I would like to comment, but I can’t since I had to redo my account because I got locked out of my original.

    If my answer isn’t helpful, I will delete in a bit, but what if you replace –

    Dim workBooksCollection as collection 'can also define as an array
    

    from the UserForm module, into Module 1 as:

    Public workBooksCollection as collection 'can also define as an array
    

    Does it help?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This might be a very stupid question :P But I found this really interessting:
This really has my stumped today. I'm sure its simple, but... Here is my
This really is a noobish question, but I honestly don't know how to solve
This is really stumping me today. I'm sure its not that hard, but I
This really seems like a bug to me, but perhaps some databinding gurus can
This really puzzled for hours, I searched all over the internet, but got no
So this really put headache on my head and I finally gave up and
hey guys having this really simple problem but cant seem to figure out have
this really silly question probably, as no one else seems to be having this
This really confuses me, I think I'm stupid but I've searched and have done

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.