+ Reply to Thread
Results 1 to 5 of 5

Make a collection of a few worksheets and check if another worksheet is in the collection

  1. #1
    Registered User
    Join Date
    07-24-2015
    Location
    Toledo, OH
    MS-Off Ver
    Microsoft Office 2010
    Posts
    85

    Make a collection of a few worksheets and check if another worksheet is in the collection

    Hi all,

    I have a workbook database that I've built. I want to create a list of the worksheets that I have created myself. I want to make the list of the worksheet codenames, specifically, because the average user can accidentally change the name of the worksheet but cannot accidentally change the codename. Once I've made this list, I want to run a macro in the Before Close event that checks every worksheet in the workbook against this list. If the worksheet is not included in the list, I want that worksheet to be deleted.

    The idea is to remove any worksheets that a user has created themselves BEFORE the workbook closes. For a hundred different reasons that I won't get into on this forum, I can't run a Workbook.Protect to prevent worksheet creation. Just trust me, I can't. So I need to build a sort of protected group of worksheets that I do not want to be deleted, and then I want to get rid of all the other ones. And it gets more complex after that: I have a userform set up where a user can legitimately add a worksheet to the database. I want to add code lines that will automatically include any worksheets created this way in this special group of worksheets.

    After doing extensive reading online, I'm thinking that a collection is the best way to go. But I have never made a collection or used a class module before. I have absolutely no idea how to do so, and not for lack of trying to understand. I apparently have a mental block for the concept.

    Please be very specific when you explain, and please don't just give me links to online tutorials, because I just spent my entire afternoon Googling and have read at least the first ten tutorials that pop up in searches about building collections, and I'm still completely confused and, to be honest, rather cranky.

    Thanks for any help you can provide!

  2. #2
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: Make a collection of a few worksheets and check if another worksheet is in the collect

    Put this subroutine in its own module then call it from the workbook_beforeclose event. You can add as many codenames to the array as necessary

    Please Login or Register  to view this content.
    Note: This process will not work if you want to allow users to add sheets on there own without access to code. To get that to work you are going to need to utilize a helper sheet (Which you could probably hide and protect) which will keep track of the sheet codenames. In that scenario you would just populate the array from the range in the helper sheet.
    Last edited by stnkynts; 10-29-2015 at 04:01 PM.
    If you are happy with my response please click the * in the lower left of my post.

  3. #3
    Registered User
    Join Date
    07-24-2015
    Location
    Toledo, OH
    MS-Off Ver
    Microsoft Office 2010
    Posts
    85

    Re: Make a collection of a few worksheets and check if another worksheet is in the collect

    Thank you for explaining it so plainly! I appreciate that. I have already decided to build a table with the worksheet names that should be included, and I will set the code to add the worksheet name and codename to the table when a user legitimately adds a worksheet. One thing, though: how do I go about adding the table information to the array? Again, please be explicit. Is this a For Each situation?

  4. #4
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: Make a collection of a few worksheets and check if another worksheet is in the collect

    One thing, though: how do I go about adding the table information to the array?
    Let's get some arbitrary details out of the way first so that you can better understand the process. Assuming:

    - Your worksheet names are going to be stored on a worksheet named "SheetNames".
    - On the sheet "SheetNames" the allowed sheet codenames will be in column A starting at row 1.
    - Each time a user adds a sheet the codename for that sheet will be transfered to the next available row in column A of "SheetNames"
    - You will have to have cell A1 be the codename for the sheet "SheetNames" so that it doesnt get deleted

    Here is how you would populate the array:

    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    07-24-2015
    Location
    Toledo, OH
    MS-Off Ver
    Microsoft Office 2010
    Posts
    85

    Re: Make a collection of a few worksheets and check if another worksheet is in the collect

    I ended up putting the list of worksheet code names into a table, because it's easier to just add a new row to the table and put the sheet name in it than it is to find the last cell in Column A. Then I just specified the array range as the first column data body range of the table. But otherwise I did this exactly the way you did it, and it works perfectly. I also have set up some code to prevent users from accidentally deleting the worksheets I want to stay put. Another ounce of idiot-proofing is in place! Thank you!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] For loop through Worksheets collection
    By amphinomos in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-31-2014, 08:23 AM
  2. [SOLVED] Performance of iteration through Words collection degrades as collection gets bigger
    By 6StringJazzer in forum Word Programming / VBA / Macros
    Replies: 7
    Last Post: 01-14-2014, 09:44 AM
  3. Replies: 2
    Last Post: 12-23-2011, 11:04 AM
  4. Define a collection of worksheets
    By mcinnes01 in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 12-22-2010, 11:34 AM
  5. Problems storing a Collection within a Collection. Is this possible?
    By Tarball in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-17-2007, 10:47 PM
  6. Removing a collection item via key name check
    By [email protected] in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-30-2006, 10:15 AM
  7. Check for an item in a collection (without looping)
    By Rob in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-10-2005, 08:06 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1