So far, we were referring to the work area as a spreadsheet. Indeed, it is a spreadsheet. In Microsoft Excel, a spreadsheet is called a worksheet. In the previous lesson, we introduced workbooks. Indeed, a workbook is a series of worksheets that are treated as a group.
In the previous lesson, we saw that, by default, when Microsoft Excel starts, it creates a workbook. That workbook is equipped with three worksheets. If you do not need all of them, you can delete those that appear useless. You can also add new worksheets as you see fit.
If you want Microsoft Excel to always start with less or more worksheets, you can change its default settings in the Excel Options dialog box accessible from the Office Button
In Lesson 1, we saw that each available worksheet is represented by a tab in the lower left area of Microsoft Excel:
A worksheet is an object of type Worksheet. The various worksheets you will use are stored in a collection called Worksheets. Another name for the collection that contains the worksheets is called Sheets. In most cases, you can use either of these two collections. Each worksheet is an object of type Worksheet.
In the previous lesson, we saw that, if you have only one workbook opened, to refer to it, you can pass an index of 1 to the Item property of the Workbooks collection to access its Workbook object. Here is an example:
Sub Exercise() Workbooks.Item(1) End Sub
You can omit the Item name if you want and you would get the same result:
Sub Exercise() Workbooks(1) End Sub
Because the worksheets of a document are part of the workbook that is opened, to support them, the Workbook class is equipped with a property named Worksheets or Sheets. Therefore, after identifying the workbook, use the period operator to access the Worksheets or the Sheets property. Here is an example:
Sub Exercise() Workbooks.Item(1).Sheets End Sub
As mentioned already, the worksheets are stored in the Worksheets collection, which is actually a class. Each worksheet can be located based on an indexed property named Item. The Item property is a natural number that starts at 1. The most left worksheet has an index of 1. The second worksheet from left has an index of 2, and so on. To access a worksheet, type one of the Worksheets or Sheets collections, followed by the period operator, followed by Item() and, between the parentheses, type the index of the worksheet you want. For example, the following code will access the second worksheet from left:
Private Sub Exercise() Workbooks.Item(1).Sheets.Item(2) End Sub
Just as we saw that you can omit the Item word on the Workbooks object, you can also omit it on the Worksheets or the Sheets object. This can be done as follows:
Sub Exercise() Workbooks.Item(1).Worksheets(2) End Sub
Or as follows:
Sub Exercise() Workbooks(1).Worksheets(2) End Sub
Each tab of a worksheet has a label known as its name. By default, the most left tab is labeled Sheet1. The second tab from left is labeled Sheet2. To programmatically refer to a worksheet using its label, call the Worksheets or the Sheets collection and pass the label of the tab you want, as a string. Here is an example that refers to the worksheet labeled Sheet3:
Sub Exercise() Workbooks.Item(1).Sheets.Item("Sheet3") End Sub
On all the code we have written so far, we were getting a worksheet from the currently opened workbook. As mentioned already, by default, when Microsoft Excel starts, it creates a default workbook and programmatically creates a Workbooks.Item(1) reference. This means that you do not have to indicate that you are referring to the current workbook: it is already available. Consequently, in your code, you can omit Workbooks.Item(1) or Workbooks(1). Here is an example:
Sub Exercise() Sheets.Item("Sheet3") End Sub
In the above code segments, we assumed that you onlywant to perform an action on a worksheet and move on. Sometimes you may want to get a reference to a worksheet. To do this, declare a variable of type Worksheet. To initialize it, access the desired worksheet from the workbook using the Item property and assign it to the variable using the Set operator. Here is an example that gets a reference to the second worksheet of the currently opened workbook and stores that reference to a variable:
Sub Exercise() Dim Second As Worksheet Set Second = Workbooks.Item(1).Sheets.Item(2) End Sub
If you have many worksheets, to select a range of worksheets, click a tab that is considered one end of the range. Press and hold Shift, then click the tab at the end of the range and release Shift.
To select worksheets at random, click one of the desired worksheets. Press and hold Ctrl. Then click each desired worksheet. When the selection has been made, release Ctrl.
To programmatically select a worksheet, access the Sheets collection, pass the name of the desired worksheet as string, and call Select. Here is an example that selects a worksheet labeled Sheet1:
Private Sub Exercise() Sheets("Sheet1").Select End Sub
The worksheet that is selected and that you are currently working on is called the active worksheet. It is programmatically identified as the ActiveSheet object (it is actually a property of the current document).
The starting worksheets are named Sheet1, Sheet2, and Sheet3. You can change any or all of these worksheet names.
To rename a worksheet:
To programmatically rename a worksheet, pass its index or its default name as a string to the Sheets (or the Worksheets) collection, then access the Name property of the collection and assign the desired name. Here is an example:
Private Sub Exercise() Sheets("Sheet1").Name = "Employees Records" End Sub
This code will change the name of the Sheet1 worksheet to Employees Records.
As we saw earlier, you can refer to, or select, a worksheet, using its name. If you had renamed a worksheet, you can use that name to select it. Here is an example that selects a worksheet named Tuition Reimbursement:
Private Sub Exercise() Sheets("Tuition Reimbursement").Select End Sub
The regular view of a worksheet is referred to as normal. It shows one large and long display of columns and cells. Microsoft Access allows you to choose among many other views.
Instead of the regular or normal view, to change how a worksheet displays, on the Ribbon, click View and click one of the buttons in the Workbook View section:
To get the regular view from Page Layout or Page Break Preview, you can click the Normal button. To get the normal view from Full Screen, you can press Esc.
By default, worksheets are positioned in a numbered format that makes it easy to count them. More often you will find that, after creating a few of them, you are not satisfied with their positions. You can change their positions if you want.
To move a worksheet, click and hold the mouse on its tab, then move the mouse in the direction of your choice. While you are moving the worksheet, the mouse pointer will turn into a white piece of paper and a small down-pointing triangle will guide you. Once the small triangle is positioned in the desired location, release the mouse.
To programmatically move a worksheet, use the Move() method of the Worksheets or the Sheets collection. The syntax of this method is:
Both arguments are optional. If you don't specify any argument, Microsoft Visual Basic would create a new workbook with one worksheet using the index passed to the collection with a copy of that worksheet. Suppose you are (already) working on a workbook that contains a few worksheets named Sheet1, Sheet2, and Sheet3. If you call this method on a collection with the index set to one of these worksheets, Microsoft Excel would make a copy of that worksheet, create a new workbook with one worksheet that contains a copy of that worksheet. For example, the following code with create a new workbook that contains a copy of the Sheet2 of the current workbook:
Private Sub CommandButton1_Click() Sheets.Item("Sheet2").Move End Sub
In this case, the name of the worksheet you are passing as argument must exist. Otherwise you would receive an error. Instead of using the name of the worksheet, you can pass the numeric index of the worksheet that you want to copy. For example, the following code will create a new workbook that contains one worksheet named Sheet3:
Private Sub CommandButton1_Click() Sheets.Item(3).Move End Sub
If calling the Item property, make sure the index is valid, otherwise you would receive an error.
To actually move a worksheet, you must specify whether it would be positioned to the left or the right of an existing worksheet. To position a worksheet to the left of a worksheet, assign it the Before factor. To position a worksheet to the left of a worksheet, assign it the After argument. Consider the following code:
Private Sub cmdMove_Click() Worksheets("Sheet3").Move After:=Worksheets("Sheet1") End Sub
This code will move the worksheet named Sheet3 to the right of a worksheet named Sheet1.
As mentioned already, when Microsoft Excel starts, by default, creates three worksheets. We also mentioned that you can change this default number in the Excel Options dialog box. You can add a new worksheet anytime if you judge it necessary.
Some documents are quite complete with just one worksheet, but others need as many worksheets as possible. The number of worksheets you use in a particular document is conditioned by your needs and the memory of your computer.
To add a new worksheet to a workbook:
To programmatically create a new worksheet, you can specify whether you want it to precede or succeed an existing worksheet. To support creating a new worksheet, call the Add() method of the Worksheets or the Sheets collection. Its syntax is:
Workbook.Sheets.Add(Before, After, Count, Type)
All of these arguments are optional. This means that you can call this method as follows:
Private Sub cmdNewWorksheet_Click() Sheets.Add End Sub
If you call the method like that, a new worksheet would be created and added to the left side of the active worksheet.
If you want to create a new worksheet on the left side of any worksheet you want, you can first select that worksheet and call the Add() method. For example, suppose you have three worksheets named Sheet1, Sheet2, and Sheet3 from left to right and you want to insert a new worksheet between Sheet2 and Sheet3, you can use code as follows:
Private Sub cmdNewWorksheet_Click() Sheets("Sheet2").Select Sheets.Add End Sub
To be more precise, you can specify whether the new worksheet will be positioned to the left or to the right of another worksheet used as reference.
As your work progresses, you will decide how many worksheets you need for your particular workbook. Just as we learned to add worksheets, you can delete or remove the worksheets you do not need anymore. Since a worksheet is not a file, when you delete a worksheet, it is permanently gone. If one or more cells of the worksheet contain data, you will receive a confirmation message to decide.
To delete a worksheet:
To programmatically remove a worksheet, call the Delete() method of its collection. When calling this method, pass the name of the worksheet you want to remove to the collection.
As mentioned already, by default, Microsoft Excel makes three worksheets available to you. We also reviewed how you can add new ones or delete some of them. Instead of deleting a worksheet, you can hide it for any reason you judge necessary. If the document contains more than one worksheet, you can hide one or more worksheets or even the entire workbook.
To visually hide a worksheet in a workbook, click any cell in that worksheet to make it active:
The worksheet’s tab will disappear from the screen although it is not deleted. To hide a group of worksheets, select them and proceed in the same way.
To visually unhide the hidden worksheets:
This would open the Unhide dialog box with a list of the the names of the hidden worksheets:
If you have more than one hidden sheet, select the desired worksheet and click OK.
You can publish the content of a worksheet to a web page. In Microsoft Excel, you can publish just one worksheet or the whole document. To perform publishing, first decide on what you want to publish, a worksheet or the whole document.
To save a file for the web, click the Office Button, position the mouse on Save As and click Other Formats. In the Save As Type combo box, select Web Page (.html). Specify whether to save the whole document or only some worksheets. Click Save. Read the message box and click Yes.
In the previous lesson, we saw how to save a workbook as a Microsoft Excel document. If you want to save it in HTML format, pass the first and the second argument of the Workbook.SaveAs() method::
In this case, pass the second argument as xlHTML. Here is an example:
Sub Exercise() Workbooks(1).SaveAs "Affiche10.htm", xlHtml End Sub
|Home||Copyright © 2007-2010, FunctionX|