Creating custom workbook object

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,834
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
My workbook contains a number of worksheets. They are classified as either fruits or vegetables, eg Apple, Orange, Cucumber, Lettuce, etc.

I want to return the values in the cell A1 from only the Fruit worksheets.

I can do the following:

Code:
Dim ws As Worksheet

For Each ws In Thisworkbook.Worksheets

    If ws.CodeName <> wksCucumber.CodeName And ws.CodeName <> wksLettuce.CodeName Then Debug.Print ws.Range("A1").Value

Next ws

However, I don't want to have to specify a condition every time. Instead, I wanted to create a single object and then use it, for example:

Code:
Public Function FruitSheets() As Collection

    Dim Coll As Collection
    Set Coll = New Collection
    
    Dim ws As Worksheet
    
    For Each ws In ThisWorkbook.Worksheets
    
       If ws.CodeName <> wksCucumber.CodeName And ws.CodeName <> wksLettuce.CodeName Then

            Coll.Add Item:=ws.CodeName
            
        End If
    
    Next ws
    
    Set FruitSheets = Coll
    
End Function

and then use it as follows:

Code:
Public Sub ChooseFruitsOnly()

    Dim ws As Worksheet
    
    For Each ws In Module1.FruitSheets
    
        Debug.Print ws.Cells(1, 1).Value
    
    Next ws

End Sub

but I get an error message:

Code:
Run-time error '424'

Object required

Is my idea possible and if so, how could I amend my code to make it work?

Thanks
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
You are not adding worksheets to your collection, you are adding the codenames of worksheets.

Try this to add the worksheet to the collection in Function FruitSheets:
VBA Code:
            Coll.Add Item:=ws
 
Upvote 0
You are not adding worksheets to your collection, you are adding the codenames of worksheets.

Try this to add the worksheet to the collection in Function FruitSheets:
VBA Code:
            Coll.Add Item:=ws

Thanks, just what I wanted.
 
Upvote 0

Forum statistics

Threads
1,215,006
Messages
6,122,666
Members
449,091
Latest member
peppernaut

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