The Worksheet object is part of the Worksheets collection, and represents a worksheet, which is a type of Excel sheet. It is related and works similarly to the Sheets object (or collection). Sheets include worksheets and charts (Chart sheets).
How to refer to a Worksheet object
Both Worksheets and Sheets can be used with either an index number or the sheet name. But using the index number with Sheets could give a different outcome if there are also Chart sheets in that workbook.
Worksheets(1) or Sheets(1)
Worksheets("SheetName") or Sheets("SheetName")
Sheet1 or MySheet (if object module or defined as object)
ActiveSheet
When working with worksheets, it is common to declare object variables in order to represent each worksheet in scope of the macro or VBA project. With that purpose, the object variable is declared as a Worksheet object and defined with the ‘Set’ statement as follows:
Examples:
Activate and Select worksheets
We can select multiple sheets but only one can be activated (or active). We can activate or select a sheet by either using the index number or sheet name (see below). That can be done referring to both the Worksheets and Sheets collection.
When manually selecting more than one sheet, only the first sheet is activated. However, if you input data with more than one sheet selected, the input will replicate in the selected sheets.
Count, Add, and Delete worksheets
The Count property shows different figures for Worksheets and Sheets if there are Chart sheets in the workbook. In the above example, there are 3 worksheets (Sheet1, Sheet2, and Sheet3), and 1 Chart sheet (Chart1), that makes a total of 4 sheets.
Worksheets.Count 'in the above example is 3 Sheets.Count 'in the above example is 4
We can add a new sheet with either the Worksheets or Sheets collections. In both cases, it will add a worksheet. To add a Chart sheet we should use Charts.Add.
There is a slight difference between Worksheets and Sheets when adding a new sheet with a given name using the Name property. Note that parenthesis after Worksheets.Add().
To delete a sheet you can use both Worksheets and Sheets too.
or
Change sheet name or color of the tab
The Name and Tab.Color properties work similarly with both Worksheet and Sheet objects.
or
or
Hide/show worksheets
An Excel sheet can either be visible (by default) or not visible (hidden or very hidden). Hidden worksheets can be manually unhidden by right-clicking any tab and selecting “Unhide…”. Very hidden worksheets can only be unhidden from the VBA project properties window or with VBA code.
Worksheets(1).Visible = xlSheetVisible
'xlSheetVisible
'xlSheetHidden
'xlSheetVeryHidden
*works similar using ‘Sheets’ instead of ‘Worksheets’
Protect/unprotect worksheets
The Protect and Unprotect methods work similarly with Worksheets and Sheets, and can be used with or without a password. When protected, all locked cells and shapes cannot be modified. To unlock or lock those particular cells or shapes, we need to refer to the corresponding Range/Cells or Shape objects (see that in the following sections).
The Protect method accepts many other attributes to specify which actions are protected or allowed in locked cells (see the full list below).
Worksheet Events
Find below some of the most common Worksheet event procedures. Note that these apply to a specific sheet in the workbook, which is clearly highlighted in the VBA project explorer window on the left.
The Activate event is triggered when moving into a worksheet and allows to run a macro upon selecting that particular worksheet.
The SelectionChange event occurs when moving from one cell or range to another within the same sheet. Target is the variable that stores the destination range or cell.
The Change event triggers when the value of a particular cell or range changes. Target is the variable that stores the range or cell which value has changed.
Other VBA Objects
No comments:
Post a Comment