In Excel VBA, “active range” usually means the cell or cells currently selected, but ActiveRange is not a built-in Excel VBA property. Use ActiveCell for the active cell, Selection for the selected object or range, and a qualified Range reference when you want code to target specific cells reliably.
What “active range” means in Excel
People often use “active range” informally to describe the cells highlighted on a worksheet. VBA distinguishes between several related ideas, however. ActiveCell represents one cell, while Selection represents the current selection, which may contain one cell, a block of cells, or even a non-cell object. A Range is an object representing one or more cells, whether or not those cells are selected.
This distinction matters because a macro that assumes a single active cell may behave differently when the user has selected a whole column or a large block. It also matters when the user has selected a chart or another object: the current Selection is then not a worksheet range. VBA does not provide an ActiveRange property that automatically handles all these cases.
For example, if B3 is the active cell inside a selection of B3:D8, ActiveCell refers to B3 and Selection refers to B3:D8. The active cell is one member of the selection, not a synonym for the entire selected range.
How Selection, ActiveCell, and Range work
Application.Selection returns the object currently selected in Excel. On a worksheet, that is commonly a Range. Application.ActiveCell returns the active cell in the active worksheet window. By contrast, Worksheet.Range("A1") identifies a cell by address on a particular worksheet, regardless of which cells the user has selected.
A range can also contain multiple areas. For instance, Ctrl-clicking separate cells can create a non-contiguous selection. A range’s Areas collection represents those separate pieces. Some operations work on such a selection, but others expect a single rectangular block, so code should not assume every selection has the same shape.
The active workbook and active sheet are user-interface state: they can change as the user switches windows or tabs. ThisWorkbook means the workbook containing the VBA code; ActiveWorkbook means the workbook currently active in Excel. They may be different, especially when a macro is stored in Personal.xlsb or an add-in. Understanding which workbook and worksheet a reference belongs to is as important as choosing between Selection and ActiveCell.
When to use Selection versus ActiveCell
Use Selection when the macro is deliberately meant to act on whatever the user selected. Examples include reporting the selected address, applying formatting to highlighted cells, or processing each selected cell. Check that the selected object is actually a range before treating it as one.
Use ActiveCell when the action should apply to exactly one cell, such as entering a status in the current cell or reading its value. If the user selects a block, the active cell is still just one cell within it. If the macro should process every selected cell, loop through the range instead of silently operating only on the active cell.
Use a worksheet-qualified range such as ws.Range("B2:B10") when the target is known in advance. This is usually the clearest and most dependable approach for repeatable tasks, because it does not depend on what happens to be selected when the macro runs. For example:
Sub SetPendingStatus()
Dim ws As Excel.Worksheet
Dim targetRange As Excel.Range
' Target the Data sheet in the workbook containing this code.
Set ws = ThisWorkbook.Worksheets("Data")
Set targetRange = ws.Range("B2:B10")
' This replaces the existing contents of these cells.
targetRange.Value = "Pending"
End Sub
This example assumes that the workbook containing the macro has a worksheet named Data. The assignment changes the contents of B2:B10, so confirm the target and save a copy before running a macro. If the intended workbook is not the one containing the code, set a worksheet variable from the correct workbook instead of assuming ThisWorkbook is the target.
A safe way to inspect the current selection
If you do want to work with the user’s current selection, first test its type. The following macro reports the address and number of cells when the selection is a worksheet range; it displays a message instead if a chart or another non-range object is selected.
Sub ReportCurrentSelection()
Dim selectedRange As Excel.Range
' Confirm that the current selection is a cell range.
If TypeOf Application.Selection Is Excel.Range Then
Set selectedRange = Application.Selection
MsgBox "Address: " & selectedRange.Address(External:=True) & vbCrLf & _
"Cell count: " & selectedRange.Cells.CountLarge
Else
MsgBox "Select worksheet cells and run the macro again."
End If
End Sub
The external address includes workbook and worksheet context, which can help identify exactly what was selected. CountLarge is useful for large ranges because it supports counts beyond the limit of the older Count property. This example only reads information; it does not change cell contents.
Why qualified references are usually more reliable
Many beginner macros use Select and Activate repeatedly: select a sheet, select a range, then change its value. That can work, but it depends on the active window and adds steps that are unnecessary for most tasks. A direct reference is clearer:
- Prefer
ws.Range("A1").Valuewhen the worksheet and address are known. - Use
ActiveCell.Valueonly when the current active cell is intentionally the target. - Use
Selectiononly when the user’s selection is part of the macro’s design. - Qualify
RangeandCellswith a worksheet variable to avoid accidentally referring to the active sheet.
For example, Range("A1") without a worksheet qualifier is generally interpreted in relation to the active sheet. If the user changes sheets, the same line can point somewhere different. Assigning Set ws = ThisWorkbook.Worksheets("Data") and then using ws.Range("A1") makes the target explicit. The same principle applies to Cells(rowNumber, columnNumber): qualify it as ws.Cells(rowNumber, columnNumber) when you mean a cell on a particular sheet.
Common problems and how to avoid them
- “Object required” or a type mismatch: The selection may be a chart or another object, not a range. Check its type before assigning it to a
Rangevariable. - The macro edits the wrong sheet: An unqualified reference may use the active sheet. Store the intended worksheet in a variable and qualify range references with it.
- Only one cell changes: The code uses
ActiveCell, but the intended target is the whole selected block. UseSelectionafter checking its type, or refer directly to the required range. - A user’s selection changes during the macro: Selecting or activating other objects can change what
SelectionorActiveCellrefers to. Store the range in an object variable before doing other work. - The macro fails on a chart sheet: The active sheet is not necessarily a worksheet. A variable declared as
Excel.Worksheetshould be assigned to an actual worksheet, not a chart sheet.
Another useful distinction is between a range address and its values. selectedRange.Address describes where the range is; selectedRange.Value reads or writes cell contents. A multi-cell range can return a two-dimensional array when its values are read, so code that expects a single value should explicitly use a single cell, such as selectedRange.Cells(1, 1).Value.
Opening and running VBA in Excel
In Excel for Microsoft 365 on Windows, press Alt+F11 to open the Visual Basic Editor. To show the Developer tab if it is hidden, use File > Options > Customize Ribbon, then select Developer. In the editor, use Insert > Module to add a standard module, paste the macro, and run it with F5 while the cursor is inside the procedure, or return to Excel and use Developer > Macros.
On a Mac, VBA is available in the desktop Excel app, though some Excel features and VBA behavior can differ from Windows. The Visual Basic Editor is available from the Developer tab when it is enabled; keyboard shortcuts can depend on the Mac keyboard and function-key settings. Excel for the web does not run or edit VBA macros. It can work with many macro-enabled workbooks, but running VBA requires desktop Excel.
Before running code that changes a workbook, save a separate copy. VBA macros cannot be undone with Ctrl+Z, even if the change appears small. Only enable and run macros from sources you trust, and make sure the workbook is saved in a macro-enabled format such as .xlsm if you need to keep VBA in that file.
Frequently Asked Questions
Is ActiveRange a valid VBA property?
No. Excel VBA does not provide a general ActiveRange property. Use ActiveCell for the active cell, Selection for the current selection, or a qualified Range reference for a known target.
How do I get the selected cells in VBA?
Check that Application.Selection is a Range, then assign it to a range variable with Set. This check matters because the user may have selected a chart or another object instead of cells.
What is the difference between ActiveCell and Selection?ActiveCell is one cell. Selection is the selected object and, on a worksheet, may be one cell, a rectangular range, or a multi-area range. In a multi-cell selection, the active cell is only one cell within that selection.
How can I refer to a cell without selecting it?
Assign the intended worksheet to a variable and use a qualified reference such as ws.Range("A1").Value. This avoids changing the selection and reduces the chance that a macro will act on the wrong active sheet.
Can VBA macros run in Excel for the web?
No. Excel for the web does not run VBA macros or provide the desktop Visual Basic Editor. Open the workbook in the Excel desktop app to run or edit its VBA code.
Related
- Excel Shortcuts You Need to Know for Faster Workflows
- Copy Excel Formulas to Google Sheets Without Errors
- How to Find a Circular Reference in Excel Step by Step
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- Excel VBA Get Current Workbook Name: Code and TipsTo get the name of the workbook currently active in Excel VBA, use ActiveWorkbook.Name. For example, assign Set…
- VBA Loop Only Through Visible Rows?Yes. In Excel VBA, loop through the rows in your data range and check whether each row is…
- Delete Rows From an Input Range in VBATo delete worksheet rows covered by a range you select in Excel VBA, use Application.InputBox with Type:=8, then…
- Excel VBA: Delete the Active Row SafelyTo delete the active row with Excel VBA, use ActiveCell.EntireRow.Delete; this removes the entire worksheet row containing the…
- Excel VBA Case Statement: How Select Case WorksExcel VBA does not have a separate Case statement; it uses Select Case ... End Select to choose…
- Excel VBA: Check Whether a Dynamic Array Is AllocatedNo—VBA arrays are not objects, so If myArray Is Nothing is not a valid way to test one.…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.