Activate and Select

The Activate and Select methods cause some confusion, and it is sometimes claimed that there is no difference between them. To understand the difference between them, you first need to understand the difference between the ActiveCell and Selection properties of the Application object. The screen in Figure 4-1 illustrates this.

Selection refers to B3:E10. ActiveCell refers to C5, the cell where data will be inserted if the user types something. ActiveCell only ever refers to a single cell, whereas Selection can refer to a single cell or a range of cells. The active cell is usually the top left-hand cell in the selection, but can be any cell in the selection, as shown in Figure 4-1. You can manually change the position of the active cell in a selection by pressing Tab, Enter, Shift+Tab, or Shift+Enter.

You can achieve the combination of selection and active cell shown in Figure 4-1 by using the following code:

Range("B3:E10").Select Range("C5").Activate

— ¿i ï Saokl - Microsoft E<ceI

-

Cl X

—- IT** Home insert Page Layout Formulas Data Ren/re^

View Developer

X

C5 T £

%

A

B C D E

F

S

H

i i

i

1

2

3

J

5

5

7

a

9

1

ID

11

12

13

Ready S |§i UfïïMiDOîîj^

ï—-'"I

Figure 4-1

If you try to activate a cell that is outside the selection, you will change the selection, and the selection will become the activated cell.

Confusion also arises because you are permitted to specify more than one cell when you use the Activate method. Excel's behavior is determined by the location of the top-left cell in the range you activate. If the top-left cell is within the current selection, the selection does not change and the top-left cell becomes active. The following example creates the screen in Figure 4-1:

Range(

"B3:

:E10")

.Select

Range(

"C5:

:Z100"

).Activate

If the top-left cell of the range you activate is not in the current selection, the range that you activate replaces the current selection, as shown by the following:

Range(

"B3

:E10"

).Select

Range(

"A2

:C5")

.Activate

In this case, the Select is overruled by the Activate and A2:C5 becomes the selection.

In this case, the Select is overruled by the Activate and A2:C5 becomes the selection.

To avoid errors, it is recommended that you don't use the Activate method to select a range of cells. If you get into the habit of using Activate instead of Select, you will get unexpected results when the top-left cell you activate is within the current selection.

0 0

Post a comment