To select a specific cell in Excel VBA, use the Range object with the Select method, for example: Range("B5").Select. For a cell on the active sheet, this is the simplest form; to select a cell on a different worksheet, qualify the range with the worksheet name, like Worksheets("Sheet2").Range("B5").Select.
Why Use Select at All?
In VBA, Select is often overused by macro recorders, but it has legitimate uses. Selecting a cell is necessary when you want to:
- Visually show the user where the active cell is (e.g., after a data entry routine).
- Perform an action that only works on the active cell, such as
ActiveCell.FormulaR1C1or applying a conditional format that depends on the selection. - Copy or cut a cell before pasting (though you can often paste without selecting).
However, for most programmatic operations—reading values, writing values, formatting—you do not need to select the cell first. Directly referencing Range("B5").Value is faster and less error-prone. Selecting triggers screen updates and can slow down macros, especially in loops.
Basic Syntax for Selecting a Cell
The core syntax is Range("A1").Select. The Range object accepts an A1-style address. You can also use the Cells property, which takes row and column numbers: Cells(3, 2).Select selects cell B3. Both work on the active worksheet.
If you want to select a cell on a specific worksheet, you must activate that worksheet first (or reference the worksheet directly). For example:
Worksheets("Sheet1").Activate
Range("A1").Select
Or, without activating, you can select on a non-active worksheet only if that worksheet is already active—actually, you cannot select a cell on an inactive worksheet without activating it first. The Select method only works on the active sheet. So you must either activate the sheet or use Worksheet.Activate before selecting.
Using Cells and Offset for Dynamic Selection
Often you need to select a cell based on variables—for instance, the last row of data. The Cells property is ideal because it accepts numbers:
Dim lastRow As Long
lastRow = Cells(Rows.Count, 1).End(xlUp).Row
Cells(lastRow, 1).Select
This selects the last non-empty cell in column A. The Offset property lets you move relative to a known cell:
Range("A1").Offset(2, 1).Select ' selects B3 (2 rows down, 1 column right)
Offset is useful when you know a starting point and need to move a variable number of rows or columns.
Selecting a Cell on Another Worksheet
To select a cell on a different worksheet, you must activate that worksheet first. Here is the correct sequence:
Worksheets("Data").Activate
Range("C10").Select
If you want to select a cell on a worksheet that is hidden, you cannot activate it—activation fails on hidden sheets. You would need to unhide it first or use a different approach (e.g., directly set values without selecting). Also, if the workbook is protected, selecting may be restricted.
Common Errors and How to Avoid Them
Two frequent errors occur when using Select:
- Select method of Range class failed – This happens when the worksheet is not active. Always activate the worksheet before selecting.
- Selecting a cell on a hidden or protected sheet – Hidden sheets cannot be activated; protected sheets may block selection. Unhide or unprotect first, or avoid selecting altogether.
Another common mistake is forgetting that Select works only on the active sheet. If your code has multiple worksheets, always specify the worksheet and activate it.
Alternatives to Select: Direct Referencing
In many cases, you can skip Select entirely. For example, to set a value in a specific cell on a specific worksheet:
Worksheets("Sheet2").Range("B5").Value = 100
This does not require activating the sheet or selecting the cell. Similarly, to read a value:
Dim val As Variant
val = Worksheets("Sheet2").Range("B5").Value
Using direct references is faster because it avoids the overhead of selection and screen updates. It also reduces the risk of runtime errors. A good rule: use Select only when you actually need to change the user’s selection or when a method requires the active cell (e.g., ActiveCell).
Selecting a Cell and Then Doing Something with It
Once you have selected a cell, you can use ActiveCell to refer to it. For example, to enter a formula into the selected cell:
Range("D5").Select
ActiveCell.Formula = "=SUM(A1:A4)"
Or to change its font:
Range("D5").Select
With ActiveCell.Font
.Bold = True
.Color = RGB(255, 0, 0)
End With
Remember that after selecting, ActiveCell is the selected cell. You can also use Selection to refer to the entire selected range, but for a single cell, ActiveCell is clearer.
Frequently Asked Questions
How do I select a cell in VBA without activating the sheet?
You cannot select a cell on a worksheet that is not active. The Select method only works on the active sheet. If you need to work with a cell on another sheet without activating it, use direct referencing (e.g., Worksheets("Sheet2").Range("A1").Value) instead of selecting.
What is the difference between Range and Cells in VBA?
Range takes an A1-style address string or a range of cells, like Range("A1:B2"). Cells takes row and column numbers, like Cells(1,1) for A1. Cells is useful when you have numeric variables for row and column. You can combine them: Range(Cells(1,1), Cells(2,2)) selects A1:B2.
Why does my macro error with “Select method of Range class failed”?
This error usually means the worksheet containing the range is not active. Ensure you have activated the correct worksheet before calling Select. For example, add Worksheets("Sheet1").Activate before Range("A1").Select. Also, check that the worksheet is not hidden or protected.
Can I select a cell in a different workbook?
Yes, but you must first activate that workbook. Use Workbooks("Book2.xlsx").Activate to make it the active workbook, then you can select cells on its active sheet. If you need to select a specific worksheet in that workbook, activate that worksheet as well.
Is it better to use Select or avoid it?
In general, avoid Select unless you specifically need to change the user’s selection. Direct referencing is faster, more reliable, and easier to debug. Use Select only when you need to visually highlight a cell or when a method requires the active cell.
How do I select a cell relative to the active cell?
Use the Offset property on the active cell. For example, to select a cell two rows down and one column right from the active cell: ActiveCell.Offset(2, 1).Select. This works regardless of which cell is currently active.
Related
- Shortcut for Refresh: F5, Ctrl+R, and More
- Maximum Number of Entries Has Been Reached? Explained
- Formatting a Cell in Excel: The Complete Guide
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- VBA Is a Number? No, It’s a LanguageNo, VBA is not a number. VBA stands for Visual Basic for Applications, a programming language built into…
- Excel VBA: Copy and Paste Range ValuesTo copy a range’s results without its formulas or formatting, assign the source range’s Value property to a…
- VBA MsgBox New Line: How to Add Line BreaksTo add a new line to a VBA MsgBox message, join the text with vbCrLf (or vbNewLine). For…
- Excel VBA: Get a String from a Cell, Safely and ReliablyTo get a cell’s value as a VBA string, read it into a Variant and convert it with…
- VBA Array Slices: How to Extract and Copy Array Parts SafelyVBA has no built-in array-slice syntax: to take a range of elements, copy them into a new array…
- Excel VBA: Copy the End Part of a String with RightTo copy the last characters of a string in Excel VBA, use the Right$ function: assign Right$(text, numberOfCharacters)…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.