Lesson 46 of 60 – Selecting and Activating Cells in VBA
77%

Selecting and Activating Cells in VBA

In VBA, the Select and Activate methods are used to work with cells, ranges, worksheets, and other Excel objects.

The Select method selects an object, while the Activate method makes a particular cell or worksheet the active object.

Note: Selecting or activating cells is useful for understanding Excel automation, but in many VBA programs you can work directly with cells without selecting them.

1. What Does Select Mean?

The Select method is used to select an Excel object.

Range("A1").Select

This selects cell A1 in the active worksheet.

After running this statement, A1 becomes the selected cell.

2. What Does Activate Mean?

The Activate method makes an object active.

Range("B2").Activate

This activates cell B2.

Only one cell can be the active cell at a time within an active worksheet.

3. Select a Single Cell

A single cell can be selected using Range.

Range("A1").Select

This selects A1.

You can select another cell by changing the address.

Range("C5").Select

4. Select Multiple Cells

A range of cells can also be selected.

Range("A1:C5").Select

This selects the range from A1 through C5.

5. Select Using Cells

The Cells property can also be used with Select.

Cells(2, 3).Select

This selects cell C2.

The first number is the row and the second number is the column.

6. Select a Complete Row

The Rows property can be used to select an entire row.

Rows(3).Select

This selects the complete third row.

7. Select a Complete Column

The Columns property can be used to select an entire column.

Columns(2).Select

This selects the complete second column, which is column B.

8. Activate a Single Cell

The Activate method can be used to activate a particular cell.

Range("D5").Activate

Cell D5 becomes the active cell.

9. Select and Activate Together

You can select a range and then activate one cell within that range.

Range("A1:C5").Select

Range("B2").Activate

The range A1:C5 is selected and B2 becomes the active cell.

10. Select a Range Using Worksheet

It is safer to specify the worksheet when selecting a range.

Worksheets("Students").Range("A1:C5").Select

This selects A1:C5 on the Students worksheet.

11. Activate a Worksheet

The Activate method can also be used with a worksheet.

Worksheets("Students").Activate

The Students worksheet becomes the active worksheet.

12. Select a Worksheet

A worksheet can be selected using the Select method.

Worksheets("Students").Select

This selects the Students worksheet.

When a worksheet is selected, it becomes the active worksheet.

13. Activate a Cell Using Cells

Cells can also be activated using row and column numbers.

Cells(5, 2).Activate

This activates cell B5.

14. Select Using a Variable

A cell address can be stored in a variable.

Dim cellAddress As String

cellAddress = "C5"

Range(cellAddress).Select

The value of cellAddress is C5, so C5 is selected.

15. Activate Using a Variable

A variable can also be used to activate a cell.

Dim rowNumber As Long

rowNumber = 10

Cells(rowNumber, 1).Activate

This activates cell A10.

16. Selecting Cells in a Loop

A loop can be used to select different cells.

Dim i As Long

For i = 1 To 5

    Cells(i, 1).Select

Next i

The code selects cells A1 through A5 one after another.

Note: Only the last selected cell remains selected after the loop finishes.

17. Activating Cells in a Loop

Cells can also be activated one by one.

Dim i As Long

For i = 1 To 5

    Cells(i, 1).Activate

Next i

Each cell in column A becomes active during the loop.

18. Selecting a Range and Formatting It

A selected range can be formatted.

Range("A1:C5").Select

Selection.Font.Bold = True

This makes the selected range bold.

However, direct formatting is usually cleaner:

Range("A1:C5").Font.Bold = True

19. Understanding Selection

After selecting an object, Excel provides the Selection object.

Range("A1:C5").Select

Selection.Font.Bold = True

Here, Selection represents the currently selected range.

Using the original range directly is generally easier to understand.

20. Select vs Activate

Method Purpose
Select Selects an object or range
Activate Makes an object active

For example:

Range("A1:C5").Select

Range("B2").Activate

A1:C5 is selected, while B2 is the active cell.

21. Selecting a Row and Activating a Cell

You can select an entire row and activate a particular cell within it.

Rows(5).Select

Cells(5, 2).Activate

Row 5 is selected and B5 becomes the active cell.

22. Selecting a Column and Activating a Cell

Columns(3).Select

Cells(5, 3).Activate

Column C is selected and C5 becomes the active cell.

23. Select and Activate with Worksheet Variables

A worksheet variable can be used to make references clearer.

Dim ws As Worksheet

Set ws = ThisWorkbook.Worksheets("Students")

ws.Activate

ws.Range("A1:C5").Select

ws.Range("B2").Activate

This activates the Students worksheet, selects A1:C5, and activates B2.

24. Selecting a Cell After Finding a Row

You can calculate a row number and then select the corresponding cell.

Dim lastRow As Long

lastRow = Cells(Rows.Count, 1).End(xlUp).Row

Cells(lastRow, 1).Select

This selects the last used cell in column A.

25. Practical Student Search Example

Suppose student IDs are stored in column A. The following example searches for a student ID and activates the matching cell.

Sub FindStudent()

    Dim i As Long

    For i = 2 To 100

        If Cells(i, 1).Value = "ST101" Then

            Cells(i, 1).Activate

            MsgBox "Student Found"

            Exit For

        End If

    Next i

End Sub

When ST101 is found, its cell becomes the active cell.

26. Common Mistakes with Select and Activate

  • Trying to select a range on an inactive worksheet.
  • Using Select unnecessarily in every VBA operation.
  • Forgetting to activate the required worksheet.
  • Confusing Select with Activate.
  • Using Selection when the original object can be referenced directly.
  • Selecting cells repeatedly inside large loops.

27. Practical Selection Example

The following example activates a worksheet and selects a student data range.

Sub SelectStudentData()

    Dim ws As Worksheet

    Set ws = ThisWorkbook.Worksheets("Students")

    ws.Activate

    ws.Range("A1:D10").Select

    MsgBox "Student Data Selected"

End Sub

28. Best Practices for Select and Activate

  • Use Select only when you actually need to change the Excel selection.
  • Use Activate when a specific object must become active.
  • Activate the required worksheet before selecting a range on it.
  • Avoid unnecessary Select and Activate operations.
  • Prefer direct object references for faster and cleaner VBA code.
  • Use worksheet variables in larger projects.
  • Be especially careful when selecting cells inside loops.

29. Complete Select and Activate Workflow

A typical workflow is:

  1. Reference the required worksheet.
  2. Activate the worksheet if necessary.
  3. Select the required range or cell.
  4. Activate a specific cell if required.
  5. Perform the required operation.
  6. Prefer direct references when selection is not necessary.
Dim ws As Worksheet

Set ws = ThisWorkbook.Worksheets("Students")

ws.Activate

ws.Range("A1:D10").Select

ws.Range("A2").Activate

30. Complete Understanding of Select and Activate

The Select method selects an Excel object, while the Activate method makes a particular object active.

Range("A1:C5").Select

Range("B2").Activate

In this example, A1:C5 is selected and B2 is the active cell.

You can also work with worksheets:

Worksheets("Students").Activate

Worksheets("Students").Range("A1:D10").Select

Although Select and Activate are useful for controlling the visible Excel selection, professional VBA programs often avoid unnecessary selection. For example:

Range("A1").Value = "Hello"
Range("A1").Font.Bold = True

This code directly works with A1 without selecting it first.

Understanding Select and Activate is still important because you will encounter these methods frequently in recorded macros and Excel VBA projects.

📌 Key Points

  • Select is used to select an Excel object.
  • Activate makes an object active.
  • Range("A1").Select selects cell A1.
  • Cells(2, 3).Select selects C2.
  • Rows(3).Select selects the complete third row.
  • Columns(2).Select selects column B.
  • A worksheet can be activated using Activate.
  • Selection represents the currently selected object.
  • Unnecessary Select and Activate operations should generally be avoided.
  • Direct object references often make VBA code cleaner and easier to maintain.

🧠 Quick Quiz

Question: Which VBA method is used to make a particular cell the active cell?