VBA Set Range to Selected Cells: How-To Guide

VBA & macros · Updated November 23, 2026

To set a range to the selected cells in VBA, use Set myRange = Selection after selecting the cells, or use Set myRange = Application.Selection if you want to be explicit. This assigns the current selection (a Range object) to a variable, allowing you to manipulate it with code. Below, we explain the nuances, best practices, and common pitfalls.

Understanding the Selection Object

In Excel VBA, Selection is a property of the Application object that returns the currently selected object in the active window. When cells are selected, Selection returns a Range object. However, if a chart, shape, or other object is selected, Selection returns a different type (e.g., ChartObject). Therefore, it’s safer to use TypeName(Selection) to verify it’s a Range before assigning it.

For example, to set a variable to the selected cells and then display the address:

Dim myRange As Range
If TypeName(Selection) = "Range" Then
    Set myRange = Selection
    MsgBox myRange.Address
Else
    MsgBox "Please select cells first."
End If

Always declare your variables with Dim and use Set when assigning an object reference. This avoids errors and makes the code clearer.

How to Set a Range to Selected Cells: Step-by-Step

Here’s the typical workflow in a macro:

  1. Select the cells you want to work with (either manually or via code).
  2. In VBA, use Set myRange = Selection to capture the selection.
  3. Now you can perform operations on myRange, such as changing values, formatting, or counting rows.

Example macro that sets the range and then fills it with the value 10:

Sub FillSelection()
    Dim rng As Range
    Set rng = Selection
    rng.Value = 10
End Sub

If you run this macro after selecting a block of cells, all selected cells will contain 10. To avoid overwriting data accidentally, always save a copy of the workbook before running macros, as macros cannot be undone with Ctrl+Z.

Why Use a Variable Instead of Selection Directly?

Using a variable (like rng) is better than repeatedly referencing Selection for several reasons:

  • Clarity: A named variable makes the code self-documenting.
  • Performance: Accessing Selection multiple times can slow down code; a variable is faster.
  • Safety: If the user changes the selection while the macro runs, a variable still points to the original range.
  • Flexibility: You can easily modify the range (e.g., Set rng = rng.Resize(5,5)) without affecting the actual selection.

For instance, to work with only the first row of the selection, you could do:

Set rng = Selection
Set firstRow = rng.Rows(1)

Common Problems and Solutions

Here are frequent issues when setting a range to selected cells:

  • Error: “Object variable or With block variable not set” – This happens if you use Set incorrectly or try to use the variable before assigning it. Always use Set for objects.
  • Selection is not a Range – If a chart or shape is selected, Selection won’t be a Range. Check with TypeName or use Selection.Cells only if you’re sure.
  • Multiple areas selected – If the user selects non-contiguous cells, Selection.Areas will have more than one area. To handle this, loop through Areas:
Dim area As Range
For Each area In Selection.Areas
    area.Value = "X"
Next area

This ensures all selected regions are processed.

Alternatives to Selection: Using CurrentRegion or InputBox

Sometimes you don’t want to rely on the user’s selection. Two common alternatives:

  • CurrentRegion – Expands from a single cell to the contiguous data block. Example:
Set rng = ActiveCell.CurrentRegion

This selects all adjacent cells with data, which is useful for tables.

  • InputBox with Type:=8 – Lets the user select a range at runtime, even if they didn’t pre-select. Example:
Set rng = Application.InputBox("Select a range", Type:=8)

This is more robust because it doesn’t depend on the current selection.

Working with the Selected Range: Practical Examples

Once you have a range variable, you can do almost anything. Here are a few concrete examples:

1. Sum the selected cells and display the result:

Dim rng As Range
Set rng = Selection
MsgBox Application.WorksheetFunction.Sum(rng)

2. Format the selection as bold with a yellow fill:

With Selection
    .Font.Bold = True
    .Interior.Color = RGB(255, 255, 0)
End With

3. Count rows and columns:

Dim rng As Range
Set rng = Selection
MsgBox "Rows: " & rng.Rows.Count & ", Columns: " & rng.Columns.Count

Remember, these operations directly affect the worksheet, so save a copy of your workbook before running them.

Frequently Asked Questions

Q: How do I set a range to the selected cells in VBA?
A: Use Set myRange = Selection. This assigns the currently selected cells to the variable myRange. Always ensure the selection is a Range object before assigning it.

Q: What’s the difference between Selection and ActiveCell?
A: Selection refers to all selected cells (which could be a single cell or a block). ActiveCell is the single cell that is currently active (the one with the thick border). If you select a range, ActiveCell is the top-left cell of that selection, but Selection is the entire range.

Q: Can I set a range to selected cells without using Selection?
A: Yes, you can use Application.InputBox to prompt the user to select a range, or use ActiveCell.CurrentRegion to get a contiguous data block. These are often more reliable than relying on the current selection.

Q: Why does my code fail when I select multiple non-adjacent cells?
A: Because Selection may contain multiple areas. You need to loop through Selection.Areas to handle each separately, as shown in the earlier example.

Q: Is there a way to set a range to the selected cells in a different worksheet?
A: Yes, you can use Worksheets("Sheet2").Range(Selection.Address) if the selection is on the active sheet. But if the selection is on another sheet, you need to activate that sheet first or use a fully qualified reference like Sheets("Data").Select before setting the range.

Safety and Best Practices

Before running any VBA macro that modifies cells, always save a copy of your workbook. Macros cannot be undone with Ctrl+Z, so a backup is essential. Additionally, consider using Application.ScreenUpdating = False to speed up code and avoid flicker, but remember to set it back to True at the end. Finally, test your macro on a small sample range first to ensure it behaves as expected.

Related

Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.

More in VBA & macros

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.