PC Review


Reply
Thread Tools Rate Thread

Custom Worksheet Collection

 
 
ExcelMonkey
Guest
Posts: n/a
 
      28th Jan 2008
Is it possible to create a custom worksheet collection in VBA. That is I
normally loop thru worksheets as follows:

For Each wrksht In ActiveWorkbook

Next

But what if I want to loop thru certain sheets which I will define by items
in listboxes chosen by the user. I know I can create a collection and add
items to it but I am not sure how access that collection using the For Loop
above.

Does this mean I have to build a class module to create a custom worksheet
class?

Any ideas?

Thanks

EM




 
Reply With Quote
 
 
 
 
Leith Ross
Guest
Posts: n/a
 
      28th Jan 2008
On Jan 27, 7:12 pm, ExcelMonkey
<ExcelMon...@discussions.microsoft.com> wrote:
> Is it possible to create a custom worksheet collection in VBA. That is I
> normally loop thru worksheets as follows:
>
> For Each wrksht In ActiveWorkbook
>
> Next
>
> But what if I want to loop thru certain sheets which I will define by items
> in listboxes chosen by the user. I know I can create a collection and add
> items to it but I am not sure how access that collection using the For Loop
> above.
>
> Does this mean I have to build a class module to create a custom worksheet
> class?
>
> Any ideas?
>
> Thanks
>
> EM


Hello Excel Monkey,

You don't need to create a Class Module for this. In this example, all
the code is in a Standard VBA Module. To make the collection available
throughout the project, the collection object variable is declared as
public. The LoadMyWorksheets macro puts the sheets you want into your
collection. You can then reference them using this format ...
MyWorksheets("sheet name")
This example loads 2 worksheets each from different workbooks.

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Module Code
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Public MyWorksheets As Object

Sub LoadMyWorksheets()

Set MyWorksheets = New Collection

With MyWorksheets
.Add ThisWorkbook.Worksheets("Sheet1"), "Sheet1"
.Add Workbooks("Code For Lookup.xls").Worksheets("Sheet4"),
"Sheet4"
End With

End Sub
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Example
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Sub ReadData()

X = MyWorksheets("Sheet4").Range("A1").Value

End Sub
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Sincerely,
Leith Ross
 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Create a collection of custom classes in another custom class kagard Microsoft Access 3 12th Jun 2011 07:57 PM
DataGridView - Custom Collection - Remove New Row from Collection =?Utf-8?B?V2ViYmVydA==?= Microsoft Dot NET 0 8th Sep 2006 12:09 PM
Can't get collection to save when using collection of custom class as property of control in VS 2005 J.Edwards Microsoft Dot NET Compact Framework 0 10th Jan 2006 04:44 AM
How to check if a worksheet exists in worksheet collection =?Utf-8?B?UmFnaHVuYW5kYW4=?= Microsoft Excel Programming 2 19th Jul 2004 06:55 AM
Custom collection: how to find item in collection using custom indexer panik Microsoft C# .NET 0 21st Aug 2003 09:01 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:06 AM.