To select a cell by its row and column number in Excel VBA, use the Cells property combined with the Select method, for example: Cells(5, 3).Select selects cell C5. This is the most direct and efficient approach, and it works in any Excel version that supports VBA.
Why Use Row and Column Numbers?
In VBA, you can refer to a cell using its address (like “C5”) or using the Range object. However, when you need to select a cell based on a calculated or variable row and column, using numbers is far more practical. For instance, if you are looping through a dataset and want to select the cell in the 10th row and 4th column, you can write Cells(10, 4).Select without having to build the address string “D10”. This approach is also faster and less error-prone, especially when the row or column numbers come from user input or other calculations.
How to Use the Cells Property
The Cells property takes two arguments: row number and column number. The row number is always a positive integer (1 for the first row), and the column number can be a number (1 for A, 2 for B, etc.) or a column letter in quotes (like “C”). Here are the basic forms:
Cells(1, 1)refers to A1.Cells(5, 3)refers to C5.Cells(2, "B")also refers to B2.
To select the cell, simply call the Select method on it. For example:
Sub SelectCellByNumbers()
' Selects cell at row 5, column 3 (C5)
Cells(5, 3).Select
End Sub
Remember that the Select method requires the worksheet to be active. If you are not on the correct sheet, you may need to activate it first, like Worksheets("Sheet1").Activate before selecting.
Selecting a Cell Relative to a Range
Sometimes you need to select a cell that is offset from a known range. For example, you might want to select the cell that is 2 rows down and 3 columns to the right of cell A1. You can combine Offset with Select:
Sub SelectOffsetCell()
' Selects cell C3 (from A1, down 2 rows, right 3 columns)
Range("A1").Offset(2, 3).Select
End Sub
Alternatively, you can use Cells with Offset on a cell reference. This is useful when you have a starting point that is not necessarily A1.
Common Scenarios and Examples
Here are a few typical situations where selecting by row and column number is handy:
- Looping through rows: In a loop, you might select a specific cell in each iteration based on a counter variable. For example, to select the cell in column B for rows 1 to 10:
Sub SelectLoop()
Dim i As Integer
For i = 1 To 10
Cells(i, 2).Select
' Do something with the selected cell
Next i
End Sub
- Selecting a cell based on user input: You can ask the user for a row and column number using
InputBoxand then select that cell.
Sub SelectUserInput()
Dim r As Long, c As Long
r = InputBox("Enter row number:")
c = InputBox("Enter column number:")
Cells(r, c).Select
End Sub
- Selecting the last used cell in a column: Use
Cells(Rows.Count, 1).End(xlUp)to find the last non-empty cell in column A, then select it.
Sub SelectLastCell()
Dim lastRow As Long
lastRow = Cells(Rows.Count, 1).End(xlUp).Row
Cells(lastRow, 1).Select
End Sub
Alternatives to Select
While Select is the direct answer to the question, in professional VBA code you often avoid Select because it slows down execution and can cause errors if the worksheet changes. Instead, you can directly act on the cell without selecting it. For example, to set a value, you can write:
Cells(5, 3).Value = "Hello"
Or to change formatting:
Cells(5, 3).Font.Bold = True
Using Select is only necessary when you want the user to see the cell highlighted, or when you need to perform an action that requires a selection, such as using the ActiveCell property. In general, prefer direct references to improve performance and reliability.
Common Errors and How to Avoid Them
Here are some typical mistakes when using Cells and Select:
- Using column letter as a number: If you write
Cells(5, "C")it works, butCells(5, C)will cause an error because C is not defined. Always use quotes or a number. - Selecting on a non-active sheet: If the worksheet is not active,
Selectwill fail. Always activate the sheet first, or useWorksheets("Sheet1").Activatebefore selecting. - Row or column out of bounds: Excel has a maximum of 1,048,576 rows and 16,384 columns. If you use a number larger than that, you’ll get an error. Use
Rows.CountandColumns.Countto avoid hardcoding. - Using
SelectwithCellsinside a loop: This can be slow. Instead, consider usingRangeto work with multiple cells at once, or use direct references.
Frequently Asked Questions
Can I select a cell using column letter instead of number?
Yes, you can use Cells(5, "C").Select to select C5. The column letter must be in double quotes. This is useful when you know the letter but not the number.
How do I select a cell relative to the active cell?
Use the Offset property on the ActiveCell. For example, ActiveCell.Offset(1, 2).Select selects the cell one row down and two columns to the right of the active cell.
What is the difference between Cells and Range?
Cells refers to a single cell by row and column numbers, while Range can refer to a cell by address (like “C5”) or a range of cells (like “A1:B10”). You can use them interchangeably for a single cell, but Cells is more flexible when using variables.
Does Select work on a protected sheet?
No, if the sheet is protected, you cannot select cells unless you unprotect it first using ActiveSheet.Unprotect (with password if required). After selecting, you may need to re-protect it.
Why does my macro stop with an error when I use Select?
This often happens because the worksheet you are trying to select on is not active. Ensure you activate the correct sheet before calling Select. Also, check that the row and column numbers are within the valid range.
Can I select an entire row or column using row/column numbers?
Yes, you can use Rows(5).Select to select the entire 5th row, and Columns(3).Select to select the entire 3rd column (C). These are equivalent to Rows("5:5") and Columns("C:C").
Final Tips for Using VBA Selection
When writing VBA code that selects cells, always save a copy of your workbook before running the macro, because Select can change the active cell and you cannot undo it with Ctrl+Z. Also, consider using Application.ScreenUpdating = False at the beginning of your macro to speed up execution, and set it back to True at the end. Remember that Select is not always necessary; often you can directly manipulate the cell without selecting it. However, when you do need to highlight a cell for the user, Cells(row, col).Select is the simplest and most reliable method.
Related
- Number 14 Balloon Drawing: Step-by-Step Guide
- SQL COUNT DISTINCT: Syntax, Examples, and Pitfalls
- Random Number 1-100: How to Generate One
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- Excel Solver VBA: Set Up and Run Optimization ModelsYes—Excel Solver can be controlled with VBA to set an objective, choose changing cells, add constraints, and run…
- VBA Copy and Paste Values Only in ExcelTo copy a range in Excel VBA and paste only its values, use Copy followed by PasteSpecial Paste:=xlPasteValues.…
- VBA MsgBox Command: Syntax, Buttons, and Examples in ExcelIn VBA, MsgBox displays a message in a dialog box and can return a value indicating which button…
- TG Macro: How to Record and Run Macros on Android SafelyTo make a macro with TG Macro, open the Android app, grant only the permissions it needs, record…
- VBA MsgBox Multiple Lines: Line Breaks and ExamplesTo show multiple lines in a VBA MsgBox, join the text with the built-in line-break constant vbCrLf. For…
- Macro Recorder Extension: Excel File Types ExplainedExcel does not need a separate macro recorder extension: Microsoft Excel for Microsoft 365 has a built-in recorder…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.