Freigeben über


Excel's Worksheets and Sheets Collection - What's the Difference?

Among the top four objects most searched for on the Microsoft Developer Network by Excel programmers are the Sheets collection and the Worksheets collection (the other two are the Range object and the Application object). As an aside, I've written new articles on programming the Range and Application objects which will be published on the Office Developer Center in January 2008.

For new Excel programmers, there might be some confusion as to the difference between the Sheets and Worksheets collections. The Worksheets collection contains the items you typically think of on an Excel worksheet: rows, columns, cells, and formulas. The Sheets collection, on the other hand, consist of not only a collection of worksheets but also other types of sheets to include Chart sheets, Excel 4.0 macro sheets (also known as XLM files) and Excel 5.0 dialog sheets (allows you to create custom dialog boxes). Chart sheets are charts that take up an entire worksheet, but not charts that are inserted as part of a worksheet. The Excel 4 and Excel 5 sheets are legacy items used to maintain backwards compatibility and ease the transition from older versions of Excel to new. And just to further muddy the waters, there is also a Charts collection that is made up of chart sheets.

The Count property of the Worksheets collection contains the number of worksheets in a workbook. The Count property of the Sheets collection contains the number of all sheets in a workbook to include chart sheets and worksheets. For example, you can add a specific number of worksheets to a workbook with the following:

Do While Worksheets.Count < 5
   ThisWorkbook.Sheets.Add
Loop

The worksheets you add may then become either a chart sheet or left as a worksheet, at which time, they become part of the Charts or Worksheets collection, respectively, or collectively become members of the Sheets collection. In the following example, you change the name of the last sheet in a workbook:

Dim wrkSheetName As String
wrkSheetName = "Projected Sales Chart"
Sheets(Sheets.Count).Name = wrkSheetName

Note that because you are using the Sheets collection, the last sheet in this workbook could be either a worksheet or a chart sheet. Given the name, it is likely a chart sheet.

As you might expect, the Sheets collection contains more methods than the Worksheets collection since it is home to more types of sheets. However, in both collections, there are a variety of methods to do such things as add, delete, copy, and move a sheet. There is ample documentation on these and other properties, methods, and events of the Worksheets and Sheets collections from the Excel portal on Office Developer Center on MSDN (https://msdn.microsoft.com/office).