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.
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.
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.
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
A range of cells can also be selected.
Range("A1:C5").Select
This selects the range from A1 through C5.
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.
The Rows property can be used to select an entire row.
Rows(3).Select
This selects the complete third row.
The Columns property can be used to select an entire column.
Columns(2).Select
This selects the complete second column, which is column B.
The Activate method can be used to activate a particular cell.
Range("D5").Activate
Cell D5 becomes the active cell.
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.
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.
The Activate method can also be used with a worksheet.
Worksheets("Students").Activate
The Students worksheet becomes the active 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.
Cells can also be activated using row and column numbers.
Cells(5, 2).Activate
This activates cell B5.
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.
A variable can also be used to activate a cell.
Dim rowNumber As Long
rowNumber = 10
Cells(rowNumber, 1).Activate
This activates cell A10.
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.
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.
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
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.
| 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.
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.
Columns(3).Select
Cells(5, 3).Activate
Column C is selected and C5 becomes the active cell.
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.
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.
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.
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
A typical workflow is:
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Students")
ws.Activate
ws.Range("A1:D10").Select
ws.Range("A2").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.
Question: Which VBA method is used to make a particular cell the active cell?