Worksheets and Workbooks Collection
- 03:21
Understanding the Worksheets and Workbooks collection of objects.
Downloads
No associated resources to download.
Transcript
The worksheet object is how Excel VBA refers to an individual worksheet in an Excel workbook.
The worksheets collection object refers to all the worksheets within a workbook worksheet and worksheets collection are both objects in Excel VBA and have their own specific properties and methods.
Let's take a look at a couple of examples.
The worksheets collection object requires us to specify a worksheet within the collection.
Here we specify the worksheet name, my worksheet name, and then specify the delete method.
This line of code will delete the worksheet named my worksheet name.
In the next example, the code worksheets 2 delete refers to the second sheet tab in an Excel workbook and uses the same delete method to remove the sheet from the collection.
We should keep in mind that Excel will renumber the remaining sheets in the workbook and worksheets 2 will not remain as worksheet 2.
If it has been deleted.
A worksheet can be identified using the active sheet object for this line of code.
To delete the required worksheet, we must ensure that the sheet is active in the front end of Excel.
The workbook object is how Excel VBA refers to an individual workbook in Excel.
The workbooks collection object refers to all the currently open workbooks in Excel and similar to worksheet and worksheet collection objects.
The workbook and workbooks collection object have specific properties and methods.
Let's take a look at an example of a workbook object and workbook collection object and their close method.
The workbooks collection object requires us to specify a workbook within the collection.
We specify the workbook named my workbook xlsx, and the close method.
This line of code will close the workbook named my workbook.
Excel xlsx exactly the same result can be achieved closing the workbook by referring to a workbook using its number.
The number given to a workbook is based on the order the workbook was opened in Excel.
Referring to a collection of workbooks can be particularly useful when we want to apply a set of code instructions to all open workbooks in Excel.
A workbook can be closed using the active workbook or this workbook object and its close method for this line of code to close the required workbook.
We must ensure that the workbook is active in the front end of Excel when the code is executed.