Create subset collection of worksheets?

gravanoc

Active Member
Joined
Oct 20, 2015
Messages
348
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
We have a defined collection in Workbooks, Worksheets, and a few others, but is it possible to make our own subset collection or alternate data structure that only holds a few sheets? My reason for this is that I have a workbook with a lot of sheets. In perspective, after a few trials it would take approximately 330 sheets for the For Each loop to take 1 second to run through, assuming that's all it does. I don't have 330 sheets, but there is a lot of branching logic going on, and I'm just trying to cut down on some of the operational time. Trying to use a collection or dictionary haven't worked for me. There are some sheets that will never meet the condition I'm checking for in my loop, so it would make sense to me to create a collection of sheets without them.
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
You can store the relevant worksheets somewhere like in a module level collection or array etc once ... Then refer to them later on throughout your code.

Something like this might work:

Declare a module level variable for storing the relevant sheets.
VBA Code:
Public oSheets As Sheets

Store the relevant sheets by index or name.
VBA Code:
Set oSheets = Sheets(Array(1, 2, 6, 15))

Then when iterating the relevant sheet in your code:
VBA Code:
Dim oSh As Worksheet
For Each oSh In oSheets
    MsgBox oSh.Name
Next
 
Upvote 0
Solution

Forum statistics

Threads
1,215,018
Messages
6,122,703
Members
449,093
Latest member
Mnur

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top