Worksheet object

The Worksheet object allows you to work with a worksheet. The Worksheets collection contains all of the worksheets in a workbook.

Creating a new worksheet

Use the Add method to create a new worksheet and add it to the Worksheets collection.

{Add a new worksheet to the Worksheets collection.}
worksheet = workbook.Worksheets.Add();

Retrieving an existing worksheet

You can retrieve a worksheet from the Worksheets collection with the Item property by the number associated with the worksheet.

{Retrieve the fourth worksheet from the Worksheets collection.}
worksheet = workbook.Worksheets.Item[4];

You can also retrieve worksheets by the name of the worksheet.

{Retrieve the worksheet named "Sheet1".}
worksheet = workbook.Worksheets.Item["Sheet1"];

Activating a worksheet

A workbook can contain several worksheets. To activate a specific worksheet, use the Activate method.

{Activate the second worksheet in the Worksheets collection.}
worksheet = workbook.Worksheets.Item[2];
worksheet.Activate();

Moving a worksheet

Use the Move method to move a worksheet to another position in the workbook. You can place a worksheet either before or after another worksheet by specifying the “Before” and “After” parameters. Specify the “Before” parameter to move a worksheet before another worksheet.

{Place a new worksheet after the fourth worksheet. The "Before" parameter refers to the fourth worksheet, and the "After" parameter is set to none.}
worksheet = workbook.Worksheets.Add();
worksheet.Move(workbook.Worksheets.Item[4], none);

Specify the “After” parameter to move a worksheet after another worksheet.

{Place a new worksheet after the first worksheet in the workbook. The "After" parameter refers to the first worksheet, and the "Before" parameter is set to none.}
worksheet = workbook.Worksheets.Add();
worksheet.Move(none, workbook.Worksheets.Item[1]);

Saving a worksheet

To save a worksheet as a new workbook, use the SaveAs method.

{Save the worksheet as a new workbook.}
worksheet.SaveAs("C:\RESM\RESM.XLS");


Documentation Feedback