The fastest way to select a range in VBA is to use the Range object with the Select method, for example Range("A1:B10").Select. However, in most macros you don’t need to select a range at all—you can work directly with the range using methods like Copy, Clear, or by reading and writing values. This article explains the different ways to select ranges, common pitfalls, and when to avoid selection altogether.
Why Selection Matters in VBA
When you record a macro in Excel, the recorder writes code that selects cells before acting on them. That’s why recorded macros are full of Select statements. But selecting a range is rarely necessary for the macro to work; it’s a side effect of how the recorder translates your mouse clicks. In fact, selecting ranges slows down your code and can cause errors if the selection changes while the macro runs. For example, if your macro selects a range and then the user clicks elsewhere before the next line runs, the macro may fail. That’s why experienced VBA developers write code that acts directly on the range without selecting it.
Basic Range Selection Syntax
The core syntax for selecting a range is:
Range("A1:B10").Select
This selects the rectangular block from A1 to B10. You can also use the Cells property to select a range based on row and column numbers:
Range(Cells(1, 1), Cells(10, 2)).Select
That selects the same range. The Cells approach is useful when you need to calculate the range dynamically, for example when the number of rows changes. To select a single cell, use Range("A1").Select or Cells(1, 1).Select.
Selecting Entire Rows, Columns, and the Used Range
Beyond a simple rectangle, you often need to select whole rows, columns, or the entire worksheet data. Here are the common patterns:
- Entire row:
Rows(3).Selectselects row 3. To select multiple rows, useRows("3:5").Select. - Entire column:
Columns("B").Selectselects column B. For multiple columns,Columns("B:D").Select. - Used range:
ActiveSheet.UsedRange.Selectselects all cells that contain data or formatting on the active sheet. This is handy when you don’t know the exact dimensions in advance. - Current region:
Range("A1").CurrentRegion.Selectselects the contiguous block of data around A1, similar to pressing Ctrl+* (Ctrl+Shift+8). This is ideal for selecting a table of data that has no blank rows or columns.
Note that UsedRange may include cells that appear empty but have formatting, so it can be larger than the actual data.
Selecting Relative Ranges (Offset and Resize)
Sometimes you need to select a range relative to a known cell. The Offset and Resize properties are perfect for this. For example, to select the cell three rows down and two columns to the right of A1:
Range("A1").Offset(3, 2).Select
That selects C4. To select a range that starts at A1 but is 5 rows tall and 3 columns wide:
Range("A1").Resize(5, 3).Select
That selects A1:C5. You can combine them, for example to select a data block starting from a header cell:
Range("A1").Offset(1, 0).Resize(10, 2).Select
That selects A2:B11, assuming you want to skip the header row. These properties are especially useful when you’re looping through data or building dynamic selections.
Selecting Non-Contiguous Ranges and Special Cells
To select multiple separate ranges at once, you can use the Union method. For example:
Union(Range("A1:A5"), Range("C1:C5")).Select
That selects A1:A5 and C1:C5 as a single selection. You can also use the SpecialCells method to select specific types of cells, such as blanks, constants, or formulas. For instance, to select all blank cells in a range:
Range("A1:C10").SpecialCells(xlCellTypeBlanks).Select
Other common SpecialCells types include xlCellTypeConstants (cells with values), xlCellTypeFormulas (cells with formulas), and xlCellTypeLastCell (the last used cell). To select the last used cell on a sheet, you can use ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Select.
Common Mistakes and How to Avoid Them
One of the most frequent errors is trying to select a range on a sheet that isn’t active. If you run Sheet2.Range("A1").Select while Sheet1 is active, Excel will throw an error because you can’t select a range on an inactive sheet. You must first activate the sheet: Sheet2.Activate followed by Range("A1").Select. Another mistake is using Select when you don’t need to, which makes the code slower and more fragile. For example, to copy a range, you can simply do Range("A1:A10").Copy Destination:=Range("B1") without selecting anything. Similarly, to clear a range, use Range("A1:A10").ClearContents. If you find yourself writing Select in a macro, ask whether you really need to change the user’s selection—usually you don’t.
Alternatives to Selection: Working Directly with Ranges
Instead of selecting a range to act on it, you can often perform the action directly. Here are common operations that don’t require selection:
- Read values:
myVar = Range("A1").Value - Write values:
Range("A1").Value = 42 - Copy:
Range("A1:A10").Copy Range("B1")(note the destination is the top-left cell of the target range) - Clear:
Range("A1:A10").Clear - Format:
Range("A1:A10").Font.Bold = True - Delete:
Range("A1:A10").Delete
Using these methods directly is faster and avoids the risk of runtime errors. If you need to loop through a range, you can use a For Each loop without selecting anything:
Dim cell As Range
For Each cell In Range("A1:A10")
cell.Value = cell.Value * 2
Next cell
This doubles every value in A1:A10 without ever changing the selection.
Frequently Asked Questions
How do I select a range that starts at a variable row and ends at the last row with data?
Use Range("A" & startRow & ":A" & Range("A" & Rows.Count).End(xlUp).Row).Select. The End(xlUp) method finds the last non-empty cell in column A, similar to pressing Ctrl+Up from the bottom. Replace startRow with your variable.
Why does my macro fail when I try to select a range on another sheet?
Because you can only select a range on the active sheet. You must first activate the target sheet with Sheets("Sheet2").Activate before using Select. Alternatively, avoid selection and work directly with the range, which doesn’t require the sheet to be active.
What’s the difference between Range.Select and Range.Activate?Select selects a range (which can be multiple cells), while Activate activates a single cell within the selection. When you select a multi-cell range, the first cell becomes the active cell. In practice, you rarely need Activate unless you want to change the active cell position.
Can I select a range using the CurrentRegion property?
Yes, Range("A1").CurrentRegion.Select selects the contiguous block of data around A1. This is useful when your data is a clean table with no blank rows or columns. If there are blank rows or columns, CurrentRegion stops at them.
Is it possible to select a range without using the Select method?
Yes, you can set the selection to a range using Application.Goto or by assigning the range to the Selection object. However, the most efficient approach is to avoid selection altogether and operate directly on the range. Selection is only necessary if you want the user to see the selected cells after the macro runs.
How do I select a range that is offset from a named cell?
Use Range("MyNamedCell").Offset(2, 3).Select to select the cell two rows down and three columns to the right of the named cell. You can also use Resize to expand the selection, for example Range("MyNamedCell").Offset(1, 0).Resize(5, 1).Select.
Related
- How to Add in Excel: Sum, AutoSum & More
- Hot Air Balloon Numbers: What They Mean & Why They Matter
- Edit Conditional Formatting Rule: How To
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- Excel Code to Insert a Row: VBA and ShortcutsTo insert a worksheet row with Excel VBA, use Rows(ActiveCell.Row).Insert Shift:=xlDown in a macro; it inserts a row…
- VBA Get Character From String: Mid, Loops & TipsTo get one character from a VBA string by position, use Mid$(text, position, 1). For example, Mid$("Excel VBA",…
- Delete an Excel Macro: Safe Steps for Windows and MacTo delete an Excel macro in Microsoft 365 for Windows, open the workbook, choose Developer > Macros, select…
- How to Open Visual Basic in Excel Using the VBA EditorIn Excel for Microsoft 365 on Windows, press Alt+F11 to open the Visual Basic Editor, or select Developer…
- Zoho Sheet VBA: How to Delete the Selected Row in ExcelZoho Sheet does not run Excel VBA macros, so there is no VBA command you can paste into…
- Excel VBA Get Month From Date: Code, Formats, and TipsIn Excel VBA, use the built-in Month function to get the month number from a date: Month(dateValue) returns…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.