PDA

View Full Version : Solved: Create New Collection



jmenche
06-06-2006, 07:55 AM
Howdy,

I have a bunch of sheets in a workbook. I want to perform certain actions (protection, recalcs, etc.) only on certain sheets. I know the best way would be to create a new collection, add the worksheet names, and then loop through the collection to perform my tasks. This would also help with errors if users rearrange sheets (I use the index now). However, I am a novice and need a little guidance on how to do.

Any help is greatly appreciated.:beerchug:

Killian
06-06-2006, 08:29 AM
Hi there,

A couple of key things to remember about collections:
Maye sure you use the "New" keyword in the declaration or when you first set the object variable
Manage the scope so you have access to it when you want to use it. The following basic example has the collection declared in the routine - the narrowest scope. It's more likely that you will want to declare it at module level so that you can write othe routines to use it. Likewise, you may want to make it global so it can be used across the whole project.Sub temp()
Dim ws As Worksheet
Dim colMySheets As New Collection
Dim str As String

'add any sheet with "copy" in its name
'to the new collection
For Each ws In ActiveWorkbook.Sheets
If InStr(ws.Name, "copy") > 0 Then
colMySheets.Add ws
End If
Next ws

'show names of sheets in new collection
For Each ws In colMySheets
str = str & ws.Name & vbLf
Next ws
MsgBox str
End SubHope that's some help...

jmenche
06-06-2006, 08:43 AM
Thanks Killian!

Where do I put it to make it global?

jmenche
06-06-2006, 09:16 AM
Another question.

How would I write the global sub if I have a set list of worksheets that I want to declare in a collection? Assume I want to add Sheet1 and Sheet2 into a new collection.

Thanks again.

Killian
06-07-2006, 05:10 AM
You make a global variable by declaring it as Public at the start of a standard module (outside any routines)Public colMySheets As New Collection
Now you can initialize your custom collection from anywhere - perhaps in the WorkBook_Open eventSub InitCollection()

Dim i As Long
'clear the collection
For i = 1 To colMySheets.Count
colMySheets.Remove 1
Next i

'add sheets to the collction by name
colMySheets.Add ThisWorkbook.Sheets("Sheet1")
colMySheets.Add ThisWorkbook.Sheets("Sheet2")

End SubYou can also refer to the collection from any other routine or module. For example, you might want to put your sheets in a list box on a form, so on the form initialize eventPrivate Sub UserForm_Initialize()
Dim ws As Worksheet

For Each ws In colMySheets
ListBox1.AddItem ws.Name
Next ws

End Sub

jmenche
06-07-2006, 05:17 AM
Thanks!!!

jmenche
06-07-2006, 02:48 PM
Killian,

I marked this as solved but have a follow up question. I did everything you said and call the sub in workbook.open. It doesn't work when I open the workbook however it does work when I just open the VBE and run the sub. Is there a scope issue here?

Any thoughts are greatly appreciated.

jmenche
06-07-2006, 02:51 PM
Killian,

Here's some more info. As I navigate through my workbook, the collection has items and then has none. How often do I need to "fill up" the collection? I assumed that once you did it it stayed full.

Thanks again.

Killian
06-08-2006, 04:08 AM
OK, I've been a bit vague, To see it working in practice, I've attached an example with some code comments so you can see how a collection can be managed.
The example views the collection through a userform, where you can also remove items from the collection or add the actrive worksheet.
The collection variable is declared as public in a standard module and a initialization routine in the same module (called from WorkBook_Open) sets it to a new collection.
From that point it is active and can be accessed by any routine. It will remain "live" all the time the standard module is loaded - which is all the time the WorkBook is open.

If your running through the code and break the program flow (or throw an error) you may find the variables lose their values, so you would need to re-initialize it in those debugging situations

You could extend the effective lifetime of the collection in two ways:
1) Use it in an AddIn that's loaded with Excel. That would make it available for the lifetime of the Excel session, so it could be used across multiple workbooks.
2) On workbook_close, save references to the collection members (on a worksheet, or in the Windows registry) and re-populate it with those items on Workbook_open

jmenche
06-08-2006, 05:11 AM
Thanks again Killian!

I was testing the workbook a lot and getting errors. I just punted and call the collection sub every time I need it.

Killian
06-09-2006, 07:02 AM
errors, you say..?