OK so, I know I can do this:
Dim ws as worksheet
Set ws = thisworkbook.worksheets("Sheet1")
and then do my fancy stuff with the ws
worksheet object
I also know I can Dim wss as worksheets
and that using worksheets("Sheet1")
returns the worksheet object. So why doesn't the following work?
Dim wss as worksheets
Dim ws as worksheet
Set wss = thisworkbook.worksheets
Set ws = wss("Sheet1")
I've also tried:
Dim wss as worksheets
Dim ws as worksheet
Set ws = thisworkbook.wss("Sheet1")
but the latter just looks like I'm trying to rename/shorten "worksheets" which seems totally wrong. I'm trying to get the worksheets of a workbook in to one worksheets object called wss. This is more about trying to understand the heirachy than anything but for functional purposes I'm trying to get wss to encompass all worksheets from workbook x so I could just do ws = wss(1)
instead of saying set ws = wb.worksheets(1)
Is that even possible or am I misunderstanding the worksheets/ worksheet relationship?
Dim wss As Sheets and Set wss = ThisWorkbook. Worksheets returns a Sheets collection object that only contains the worksheets (and not charts or macro sheets). And you can use Debug. Print wss(1). Name for example.
The difference between Sheets and WorksheetsWorksheet – the sheet with the gridlines and cells. Chart – the sheet which contains a single chart. DialogSheet – an Excel 5 dialog sheet. These are effectively defunct as they have been replaced by VBA UserForms.
In the code examples in the next sections the name of the worksheet variable is kept simple to 'ws' and its declaration is ignored: Dim ws As Worksheet Set ws =
Easy VBA Macro to Switch Between Sheets/Tabs You can create a macro and put an icon in the Quick Access Toolbar so that whenever you click on that icon, it would switch the sheets. Below is the macro code that will switch between Sheet1 and Sheet3 whenever it is run.
In VBA, you have two collections that can be a bit confusing at times. In a workbook, you can have worksheets and as well as chart sheets. The ‘Worksheets’ collection would refer to the collection of all the worksheet objects in a workbook. The ‘Sheets’ collection would refer to all the worksheets as well as chart sheets in the workbook.
And if the sheet name has been changed, your code wouldn’t work until you change the name of the worksheet in the VBA code as well. To tackle this problem, you can use the code name of the worksheet (instead of the regular name that we have been using so far).
To be able to operate on a worksheet using VBA you have to specify which worksheet. Below discusses the many ways you can do this. The sample code declares and sets variables for the worksheet as a best practice. If you use the Code VBAadd-in using Set will add the declaration automatically.
to your surprise, you do need to declare variable for workbook and worksheet in excel 2007 or later version. Just add single line expression. Remove everything else and enjoy. But why to select a sheet? selection of sheets is now old fashioned for calculation and manipulation.
you must declare wss
as a Sheets object
Dim wss As Sheets
Dim ws As Worksheet
Set wss = ThisWorkbook.Worksheets
Set ws = wss("Sheet1")
this is because Worksheets property of Workbook
object returns a Sheets
collection, i.e. a collection that contains both Worksheets
and Charts
object of the workbook
Should you need a collection of your Workbook
Worksheets
only (not Charts
) to be called like ws = wss(1) or the likes then you could adopt the following workaround with Collection
object
Option Explicit
Sub main()
Dim wss As Collection
Dim ws As Worksheet
Set wss = GetWorkSheets
Set ws = wss("Sheet1")
Set ws = wss(1)
End Sub
Function GetWorkSheets() As Collection
Dim wss As New Collection
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
wss.add ws, ws.Name
Next ws
Set GetWorkSheets = wss
End Function
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With