Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Excel VBA Selected Range: A How-To Guide

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

To work with the selected range in Excel VBA, use Selection or Application.Selection. For example, Selection.Value = "Done" writes to every cell the user has highlighted. If you need the address or dimensions, use Selection.Address or Selection.Rows.Count. The key is to check that the selection is actually a range before you act on it, because Selection can also be a chart, shape, or nothing at all.

This guide shows you how to read, write, loop through, and safely handle the selected range in Excel VBA. It assumes you can open the Visual Basic Editor and run a macro. The exact menu labels in the VBA Editor can differ slightly by Excel version and region, but the core objects and methods are the same.

What You'll Need

  • Excel for Windows or Mac with macros enabled.
  • Access to the Developer tab (File > Options > Customize Ribbon > check Developer) or press Alt+F11 to open the VBA Editor.
  • A workbook saved as .xlsm if you want to keep your macro.
  • Basic familiarity with inserting a module and running a macro.

Step-by-Step

  1. Open the Visual Basic Editor. Press Alt+F11 (Windows) or Fn+Alt+F11 (some Mac keyboards). In the Editor, go to Insert > Module. A blank module appears in the Project Explorer.
  2. Write a simple macro that reads the selection. In the module, type:
    Sub ReadSelection()
        If TypeName(Selection) = "Range" Then
            MsgBox Selection.Address
        Else
            MsgBox "Please select a range of cells."
        End If
    End Sub

    This checks the type first, then shows the address of the selected cells. The TypeName check prevents errors when a chart or shape is selected.
  3. Run the macro. Close the Editor, select a block of cells on the worksheet, then press Alt+F8, choose ReadSelection, and click Run. A message box shows the address, such as $B$2:$D$5.
  4. Write to the selected range. Replace the message box line with an assignment. For example, Selection.Value = "Reviewed" fills every selected cell with the word Reviewed. If you only want to change the active cell within the selection, use ActiveCell.Value = "Reviewed" instead.
  5. Loop through the selected cells. To process each cell individually, use a For Each loop:
    Dim cell As Range
    For Each cell In Selection
        cell.Value = cell.Value * 2
    Next cell

    This doubles every numeric value in the selection. Non-numeric cells will cause a type mismatch error, so add a check such as If IsNumeric(cell.Value) Then before the calculation.
  6. Work with rows and columns. Use Selection.Rows.Count and Selection.Columns.Count to get the size. To select the entire row of the active cell, use ActiveCell.EntireRow.Select. To resize the selection, use Selection.Resize(2, 3).Select, which changes the selection to two rows by three columns starting from the top-left cell.
  7. Store the selection in a Range variable. Declare Dim rng As Range and then Set rng = Selection. This lets you refer to the original selection later even if the user clicks elsewhere. Without Set, the variable stays empty and you get error 91 (Object variable or With block variable not set).
  8. Handle multiple areas. If the user selects non-adjacent cells (for example, hold Ctrl and click), Selection.Areas.Count is greater than 1. Loop through Selection.Areas to process each block separately. A simple Selection.Value assignment still works across areas, but row and column counts refer only to the first area.

Common Mistakes

  • Assuming Selection is always a Range. If a chart sheet or shape is selected, Selection.Value fails. Always test TypeName(Selection) = "Range" first.
  • Forgetting Set. Writing rng = Selection instead of Set rng = Selection causes error 91.
  • Using Selection inside event code without checking. In a Worksheet_SelectionChange event, the selection has already changed. Capture the target range from the event parameter instead of relying on Selection.
  • Assuming a single area. Non-adjacent selections break loops that expect one rectangular block. Check Areas.Count when the user might use Ctrl+click.
  • Leaving the macro recorder's output unedited. Recorded macros often hard-code addresses like Range("A1:C5").Select. Replace those with Selection if you want the macro to act on whatever the user highlights.

Tips

  • Use Application.Selection when you want to be explicit, though Selection alone works in most modules.
  • To avoid slowing down large selections, read the range into a Variant array with arr = Selection.Value, process the array in memory, then write it back with Selection.Value = arr. This is much faster than looping cell by cell on thousands of rows.
  • Use Selection.SpecialCells(xlCellTypeVisible) when the range includes hidden rows and you only want visible cells.
  • Add On Error GoTo handling around selection code so users see a friendly message instead of a debug prompt.
  • If you need the selection to persist after the macro ends, store its address in a defined name or a cell, because the next click changes Selection.

Troubleshooting

If the macro does nothing, check that you selected cells before running it and that the code is in a standard module, not a sheet module. If you get error 91, add Set before assigning to a Range variable. If you get error 424 (Object required), confirm the selection is a range using TypeName. If the macro writes to the wrong cells, remember that Selection refers to what is highlighted at the moment the line runs; clicking elsewhere during a break can change it. If the macro is slow on a large selection, switch to the array method described in Tips. If nothing happens at all, check that macros are enabled in the Trust Center and that the workbook is not open in Protected View.

Frequently Asked Questions

What is the difference between Selection and ActiveCell?

Selection is the entire highlighted range. ActiveCell is the single cell that would receive typing, usually the top-left cell of the selection.

Can I use Selection in a UDF called from a worksheet cell?

No. User-defined functions called from worksheet formulas cannot change the selection or read it reliably. Use a macro instead.

How do I select a range from VBA rather than read it?

Use Range("A1:C10").Select or Worksheets("Sheet1").Range("A1:C10").Select. The sheet must be active for Select to work.

Why does Selection.Rows.Count return 1 for a multi-row selection?

This happens with non-adjacent selections. Only the first area is counted. Loop through Selection.Areas to handle each block.

How do I get the values of the selected range into an array?

Use Dim arr As Variant then arr = Selection.Value. The array is 1-based and two-dimensional. Write it back with Selection.Value = arr.

Does Selection work the same on Excel for Mac?

Yes, the object model is the same. Keyboard shortcuts and some menu labels differ, but Selection behaves identically in VBA on Windows and Mac.