In Excel VBA, use Cells(rowNumber, columnNumber) to identify a cell, then call Select on that cell or on a range built from two Cells references. For example, Worksheets("Data").Range(Worksheets("Data").Cells(2, 1), Worksheets("Data").Cells(10, 3)).Select identifies A2:C10—but the Data sheet must be active before VBA can select it.
How the Cells method works
Cells is a worksheet property that returns a range of cells. Its two arguments are the row number and column number, in that order. Therefore, Cells(1, 1) means A1, Cells(5, 2) means B5, and Cells(12, 4) means D12. The numeric column index is often convenient when a macro calculates a row or column rather than using a fixed address.
On its own, Cells(5, 2) refers to one cell; it does not select it. Add .Select to make that cell the current selection, or use the reference as part of a larger expression. For example, Range(Cells(2, 1), Cells(10, 3)) describes a rectangular range whose upper-left corner is A2 and lower-right corner is C10.
In that two-endpoint form, Range receives the first and last cells. Excel includes every cell in the rectangle between them. The endpoints need not be supplied in ascending order, but writing the top-left endpoint first and bottom-right endpoint second makes the intended dimensions easier to read. In VBA, Cells and Range are object references, so the worksheet they belong to matters.
Selecting one cell with Cells
For a single cell, the pattern is simple: qualify Cells with the worksheet, then select it after activating that worksheet. The following example uses the sheet named Data in the workbook that contains the code:
Sub SelectOneCell()
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Data")
ws.Activate
ws.Cells(5, 2).Select ' Selects B5
End Sub
The row and column arguments can be variables, too. If rowNum is 5 and colNum is 2, ws.Cells(rowNum, colNum) points to B5. This is useful in loops, where the row changes on each pass, or when a column number is stored in a setting. Both arguments must resolve to valid worksheet positions; an invalid row or column value causes a runtime error.
Selection is an interface action: the user sees the active cell change. It does not, by itself, edit the cell, copy it, or change its formatting. To write to the cell, assign a value or formula to its property instead; that distinction is important when adapting recorded macros.
Selecting a rectangular range with two Cells references
To select multiple cells, pass two worksheet-qualified Cells references to that same worksheet’s Range property. This example selects A2:C10 on Data:
Sub SelectRectangle()
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Data")
ws.Activate
ws.Range(ws.Cells(2, 1), ws.Cells(10, 3)).Select ' A2:C10
End Sub
Read the expression from the outside in: ws.Range(...) creates the range on ws; the first inner reference supplies its starting cell, and the second supplies its ending cell. Using ws in all three places keeps the range endpoints and the parent range on the same worksheet. This pattern is clearer and safer than mixing an unqualified Cells reference with a qualified Range.
The row and column values can be variables. For instance, a start row of 2, end row of 10, start column of 1, and end column of 3 produce A2:C10. This makes the same pattern reusable for different table sizes. Keep the endpoint variables as row and column numbers, not cell-address strings; the latter require a different construction.
Why worksheet qualification matters
Inside a standard VBA module, an expression such as Cells(2, 1) without a worksheet qualifier refers to the active sheet. The active sheet can change when a user clicks another tab or when earlier macro steps activate a different sheet. As a result, a line that appears to target Data might actually read from or act on another sheet.
Qualify references consistently, as in ws.Cells(2, 1) and ws.Range(...). Set ws to the intended worksheet before using it. ThisWorkbook means the workbook containing the VBA code; ActiveWorkbook means the workbook currently active in Excel. They can be different, for example when code is stored in a separate macro workbook, so choose deliberately.
There is a separate rule for selection: Excel can select a range only on the active worksheet in the active workbook. If the target sheet is not active, a properly qualified range reference can still be created and used for many operations, but calling .Select on it can fail. Activate the target sheet first when selection is truly required. If the target workbook is not active, activate that workbook as well before selecting its sheet.
When to select—and when to work with the range directly
Many macros do not need to change the selection at all. Selecting a range can make a macro depend on the active window, interrupt the user’s current selection, and add extra steps. If the goal is to enter a value, format cells, or inspect data, use the range object directly. For example, this writes the text Ready into B5 without selecting it:
Sub WriteWithoutSelecting()
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Data")
ws.Cells(5, 2).Value = "Ready" ' Writes to B5
End Sub
Likewise, code can set a range’s formatting or read its values through the qualified range reference. Direct operations are usually easier to maintain because they state exactly which sheet and cells the macro will use, regardless of the selection. Select only when the visible selection is part of the task—for example, when a macro is intended to guide a user to an area to review.
If you do need the selection to be visible, keep the activation close to the selection and use the same worksheet variable for both. Avoid relying on whichever tab happens to be active when the macro starts. This makes the intended behavior apparent to someone reviewing or revising the code later.
Selecting a range with a variable last row
A common task is to select a column’s data from a known starting row through its last populated row. Suppose column A contains a header in row 1 and records begin in row 2. The code below finds the last nonblank cell in column A by starting at the bottom of the worksheet and moving upward:
Sub SelectUsedRows()
Dim ws As Worksheet
Dim lastRow As Long
Set ws = ThisWorkbook.Worksheets("Data")
lastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
If lastRow >= 2 Then
ws.Activate
ws.Range(ws.Cells(2, 1), ws.Cells(lastRow, 3)).Select
End If
End Sub
Here, the last-row search uses column A, while the selected rectangle spans columns A through C. The condition prevents the macro from selecting a supposed data area when the only nonblank entry in column A is the header. If column A can contain gaps or be blank while other columns contain records, choose a more reliable column for the last-row check; this method finds the last occupied cell in the specified column, not the last row containing anything anywhere on the sheet.
The selected range ends at the row returned by the search. If the list is empty, End(xlUp) can return row 1, which is why the example checks the result before selecting. For a real workbook, adapt the start row, lookup column, and selected columns to match the table layout rather than assuming every worksheet uses the same structure.
Common errors and how to avoid them
- “Select method of Range class failed.” Usually the range’s worksheet is not active, or the workbook containing it is not active. Activate the correct workbook and worksheet before calling
Select, or avoid selection and operate on the range directly. - The wrong sheet is affected. An unqualified
CellsorRangemay point at the active sheet. Set a worksheet variable and qualify every endpoint and the range itself. - The selected rectangle has the wrong dimensions. Remember that the first argument to
Cellsis the row and the second is the column. A reversed pair such asCells(2, 10)means J2, not B10. - A variable endpoint is zero or out of bounds. Check how row and column variables are calculated, and ensure they fall within the worksheet’s valid range. A row number of zero is not a valid cell row.
- The macro selects only a header or includes unwanted rows. Confirm the start row and the column used to find the last row. A last-row calculation only describes the chosen lookup column, so blanks in that column can make it unsuitable for the data.
When troubleshooting, inspect the worksheet variable and the numeric row and column values immediately before the selection line. Temporarily using Debug.Print in the Immediate window can help reveal unexpected values without changing the workbook. Also check whether the sheet is hidden: a hidden worksheet cannot be made the active sheet for a normal visible selection until it is made visible.
Safe macro use and practical workflow
Before running a macro that changes a workbook, save a copy. Excel VBA macros cannot be undone with Ctrl+Z, even when a change looks small. Use a macro-enabled workbook format such as .xlsm if the VBA code needs to be saved with the workbook, and run macros only from workbooks you trust.
A reliable workflow is to identify the target workbook and worksheet, declare a worksheet variable, set it explicitly, calculate or confirm the row and column numbers, and then build the range. Decide last whether the task genuinely requires .Select. In Microsoft 365 for Windows, macros can be run from Developer > Macros by selecting the macro and choosing Run; if the Developer tab is not shown, it can be enabled in Excel Options. Excel for Mac also supports VBA macros, though keyboard shortcuts and some interface details differ; Excel for the web cannot create, run, or edit VBA macros.
For a macro that only edits values or formats, leave the user’s selection alone. For a macro that must show a selection, activate the target sheet and, if needed, the target workbook first. This division keeps code both predictable and clear about whether its purpose is to change data or guide the user’s attention.
Frequently Asked Questions
How do I select a range using Cells in VBA? Use ws.Range(ws.Cells(startRow, startColumn), ws.Cells(endRow, endColumn)).Select, where ws is the target worksheet. Activate that worksheet before selecting the range.
Does Cells use row first or column first? Row comes first and column comes second. Thus, Cells(3, 2) refers to B3: row 3, column 2.
Can I use Cells without Select? Yes. A reference such as ws.Cells(5, 2).Value can read or write B5 directly. Avoiding Select is generally preferable when the macro does not need to show a selection.
Why does selecting a qualified range still fail? Qualification identifies the worksheet, but it does not make that sheet active. Excel requires a selected range to be on the active sheet, so activate the target worksheet first or use the range directly without selecting it.
Can Cells use column letters? The clearest and most dependable pattern is to use a numeric column index, such as Cells(1, 3) for C1. For a fixed address written with letters, use a qualified Range("C1"); for calculated positions, use numeric Cells arguments.
Related
- Excel Trendlines with Dates: How to Make Them Work
- Excel Text to Columns Shortcut Key for Windows and Mac
- Excel Text to Columns Using Enter: Split Line Breaks
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- VBA MsgBox Location: Where It Appears and How to Control ItA VBA MsgBox has no built-in argument for setting its screen coordinates. In Excel for Microsoft 365 on…
- Excel VBA: Find the Last Used Row in Any WorksheetTo find the last row containing content anywhere on a worksheet in Excel VBA, use Range.Find with SearchOrder:=xlByRows…
- Excel VBA InputBox Types and When to Use EachExcel VBA has two commonly used input boxes: the VBA InputBox function, which returns text, and Application.InputBox, which…
- Input Box in Excel VBA: Use InputBox and Get DataIn Excel VBA, use InputBox to ask for a text value, or Application.InputBox when you want to restrict…
- VBA Copy and Paste Special: Examples and OptionsIn Excel VBA, copy a range and paste only selected content with Range.Copy followed by Range.PasteSpecial. For example,…
- Excel VBA Open Workbook Dialog: A Practical How-ToTo display a workbook picker in Excel VBA, call Application.GetOpenFilename, check whether the user cancelled, and pass the…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.