Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VBA dim ws as worksheets (not worksheet)

Tags:

excel

vba

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?

like image 202
jamheadart Avatar asked Sep 24 '16 14:09

jamheadart


People also ask

What does dim ws As worksheet mean?

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.

What's the difference between sheets and worksheets VBA?

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.

What does WS mean in VBA?

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 =

How do I switch between worksheets in Excel VBA?

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.

What is the difference between 'sheets' and 'workbooks' in VBA?

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.

Why is my Excel VBA code not working after changing sheet name?

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).

How do I use VBA to operate on a worksheet?

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.

How to declare variable for workbook and worksheet in Excel?

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.


1 Answers

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
like image 197
user3598756 Avatar answered Sep 24 '22 06:09

user3598756