Obtaining information in the Immediate Window

So far you have used the Immediate window to perform actions. These actions could have been performed manually by clicking the mouse in various areas of the worksheet and entering data. The Immediate window also allows you to ask questions. Suppose you want to find out the answers to the following: " What cells are currently selected?", "What is the value of the active cell?", "What's the name of the active sheet?", "What's the number of the current window?" When working in the Immediate window, you can easily get answers to these and other questions.

In the preceding exercise, you entered several instructions. Let's return to the Immediate window to ask some questions. Excel remembers the instructions entered in the Immediate window even after you close this window. The contents of the Immediate window are automatically deleted when you exit Microsoft Excel.

1. Click the mouse in the second line of the Immediate window where you previously entered the instruction Range("A1:A4").Select.

2. Press Enter to have Excel reselect cells A1:A4.

3. Click in the new line of the Immediate window and enter the following question:

?Selection.Address

When you press Enter, Excel will not select anything in the worksheet. Instead, it will display the result of the instruction on a separate line in the Immediate window. In this case, Excel returns the absolute address of the cells that are currently selected ($A$1:$A$4). The question mark (?) tells Excel to display the result of the instruction in the Immediate window. Instead of the question mark, you can use the Print keyword. Let's now ask for the name of the worksheet using the Print keyword.

4. In a new line in the Immediate window, enter the following question: Print ActiveWorkbook.Name

When you press Enter, Excel enters the name of the active workbook on a new line in the Immediate window.

How about finding the name of the application? Who's the parent of Chap02.xls?

5. In a new line in the Immediate window, enter the following question: ?Application.Name

Excel will reveal its full name: Microsoft Excel.

The Immediate window can also be used when you require a quick calculation.

6. In a new line in the Immediate window, enter the following question:

When you press Enter, Excel shows the result of the division on the next line. But what if you want to know right away the result of 3+2 and 12*8? Instead of entering these instructions on separate lines, you can enter them on one line, as in the following example: ?3+2:?12*8.

Notice the colon separating the two blocks of instructions. When you press the Enter key, Excel displays the results 5, 96 on separate lines in the Immediate window.

Below are all the instructions you entered in the Immediate window including Excel's answers to your questions:

Worksheets("Sheet2").Activate

Range("A1:A4").Select

Selection.ClearContents

ActiveCell.Select

?Selection.Address

Print ActiveWorkbook.Name

Chap02.xls

?Application.Name

Microsoft Excel

To delete the instructions from the Immediate window, highlight all the lines and press Delete.

0 0

Post a comment