tiredofit
Well-known Member
- Joined
- Apr 11, 2013
- Messages
- 1,834
- Office Version
- 365
- 2019
- Platform
- 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:
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:
and then use it as follows:
but I get an error message:
Is my idea possible and if so, how could I amend my code to make it work?
Thanks
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