Download the Practice Workbook
How to Select a Cell with VBA in Excel
Method 1 – Select a Cell of the Active Worksheet with VBA in Excel
We have a workbook called Workbook1. There are three worksheets called Sheet1, Sheet2, and Sheet3 in the workbook. The active worksheet is Sheet1.
You can use the following line of code to select any cell (C5 in this example) in the active worksheet:
⧭ VBA Code:
ActiveSheet.Range("C5").Select
OR
ActiveSheet.Cells(5,3).Select
⧭ Output:
The code will select the cell C5 of the active worksheet Sheet1 of Workbook1.
Method 2 – Select a Cell of the Active Workbook but Not of the Active Worksheet with VBA in Excel
Now, let’s select a cell of the active workbook, but not of the active worksheet. Our active worksheet is Sheet1, but we’ll select cell C5 of Sheet2.
Use the following line of code:
⧭ VBA Code:
Application.Goto Sheets("Sheet2").Range("C5")
OR
Application.Goto Sheets("Sheet2").Cells(5,3)
OR
Sheets("Sheet2").Activate
Range("C5").Select
⧭ Output:
This will select the cell C5 of the worksheet Sheet2 of the active workbook Workbook1.
Method 3 – Select a Cell Outside of the Active Workbook with VBA in Excel
Our active workbook is Workbook1. We have another workbook called Workbook2 in the same folder.
Let’s select cell C5 of Sheet1 of Workbook2.
Use this code:
⧭ VBA Code:
Application.Goto Workbooks("Workbook2.xlsx").Sheets("Sheet1").Range("C5")
OR
Application.Goto Workbooks("Workbook2.xlsx").Sheets("Sheet1").Cells(5,3)
OR
Workbooks("Workbook2.xlsx").Activate
Sheets("Sheet1").Select
⧭ Output:
Run the code and it’ll select cell C5 of Sheet1 of Workbook2.
Method 4 – Select a Range of Cells with VBA in Excel
We’ll select a range of cells (B4:C13).
For the active worksheet selection, use:
⧭ VBA Code:
Range("B4:C13").Select
⧭ Output
It’ll select cells B4:C13 of the active worksheet Sheet1 of Workbook1.
For another sheet in the active workbook (Sheet2 in the example), use:
⧭ VBA Code:
Application.Goto Sheets("Sheet2").Range("B4:C13")
⧭ Output:
It’ll select cells B4:C13 of Sheet2 of the active workbook Workbook1.
For a different workbook in the same folder:
⧭ VBA Code:
Application.Goto Workbooks("Workbook2.xlsx").Sheets("Sheet2").Range("B4:C13")
⧭ Output:
It’ll select the range B4:C13 of Sheet1 of Workbook2.
Method 5 – Select Cells in a Named Range with VBA in Excel
In the active sheet Sheet1 of Workbook1, we have a Named Range called ABC which consists of the range B4:C13.
To select the Named Range ABC, use this line of code:
⧭ VBA Code:
Range("ABC").Select
⧭ Output:
It’ll select the Named Range (B4:C13) of Sheet1 of Workbook1.
Method 6 – Select a Cell Relative to Another Cell with VBA in Excel
We’ll select the cell to 2 rows down and 3 columns right from cell C5 in the active worksheet Sheet1 of Workbook1.
Use the following line of code:
⧭ VBA Code:
Range("C5").Offset(2, 3).Select
OR
Cells(5,3).Offset(2, 3).Select
⧭ Output:
It’ll select cell F7, the cell to 2 rows down and 3 columns right from cell C5.
These are great instructions! Let’s say I have a horizontal range already selected by the user. For example it could be A2:G2. How can I know the rightmost cell of the selection range? In my example it would be G2. My macro will do something with the users’ current selection when invoked, but then I want to focus the cell on the rightmost cell.
Hello KEVIN,
Hope you are doing well. You can follow the procedures below to get the address of the rightmost cell.
Here, we have taken the following dataset into our consideration. Suppose, the user selected the header of the dataset which is A3:G3.
• Go to the Developer tab >> Visual Basic option to open the Visual Basic Editor window.
• Use the following code.
• Press F5.
Then, you will get the following message with the rightmost cell of the selected range $G$3.