To convert a selection, variable, or object to a Range in VBA, you use the Range property of the Worksheet or Application object, or you assign an existing object to a Range variable with the Set keyword. For example, Set myRange = Range("A1:B10") converts those cells into a Range object you can manipulate. This article explains the different ways to create or convert to a Range in Excel VBA, including how to turn a selection, a string address, or a named range into a usable Range variable.
What Is a Range Object in VBA?
In Excel VBA, a Range object represents a cell, a row, a column, or a block of cells. It is the fundamental building block for reading and writing cell values, formatting, and performing calculations in code. When you write Range("A1"), VBA interprets the string “A1” as an address and returns a Range object pointing to that cell. The term “convert to range” usually means taking something else—like a selection, a string, or an object—and turning it into a Range so you can use its properties and methods.
How to Convert a Selection to a Range
The most common conversion is turning the user’s current selection into a Range variable. This is done with the Selection object, which returns a Range when a cell or range is selected. Here’s a simple example:
Dim rng As Range
Set rng = Selection
MsgBox rng.Address
This works because Selection already returns a Range object when cells are selected. If the selection is a chart or shape, you’ll get an error, so it’s wise to check with TypeName(Selection) before assigning. For a safer conversion, use ActiveSheet.Range(Selection.Address) to force a Range from the address string.
Converting a String Address to a Range
Often you have a text address like “B2:D10” and need to turn it into a Range. The Range property accepts a string directly:
Dim myRange As Range
Set myRange = Range("B2:D10")
You can also build the address dynamically using variables:
Dim lastRow As Long
lastRow = Cells(Rows.Count, 1).End(xlUp).Row
Set myRange = Range("A1:C" & lastRow)
This converts a constructed string into a Range. Note that Range without a worksheet qualifier refers to the active sheet. To be explicit, use Worksheets("Sheet1").Range(...).
Using the Set Keyword to Assign Ranges
In VBA, you must use Set when assigning an object reference to a variable. This is different from assigning a value like a number or string. For example:
Dim rng1 As Range
Set rng1 = Range("A1:A10")
If you forget Set, you’ll get a runtime error. Once assigned, you can use rng1.Value, rng1.Font.Bold = True, or pass it to other procedures. The Set keyword effectively “converts” the reference into a named variable you can reuse.
Converting a Named Range to a Range Object
Named ranges (defined names) can also be converted to Range objects. You refer to them by name:
Set myRange = Range("MyNamedRange")
Or using the Names collection:
Set myRange = ThisWorkbook.Names("MyNamedRange").RefersToRange
The RefersToRange property returns the actual Range object behind the name. This is useful when you need to work with the range’s cells directly. If the name refers to a constant or formula, RefersToRange may return Nothing, so check for Nothing before using.
Common Mistakes and How to Avoid Them
One frequent error is using Range without a worksheet qualifier when the active sheet isn’t the intended one. Always specify the worksheet:
Set myRange = Worksheets("Data").Range("A1:B10")
Another mistake is trying to assign a Range to a variable without Set, which causes a “Object required” error. Also, beware of using Selection when the selection isn’t a range—for example, if a chart is selected. Check with If TypeName(Selection) = "Range" Then before converting.
When you run a macro that modifies ranges, remember that Ctrl+Z cannot undo VBA changes. Always save a copy of your workbook before running a macro, especially if it writes or deletes data.
Alternatives: Using Cells and Offset
Instead of converting a string to a Range, you can use the Cells property to refer to a single cell by row and column numbers, and Offset to move relative to a base range. For example:
Set myRange = Cells(1, 1) ' Returns Range A1
Set myRange = Range("A1").Offset(2, 3) ' Returns D3
These methods are often more flexible than building address strings, especially in loops. They return Range objects directly, so no conversion is needed. You can combine them with Resize to get a larger block:
Set myRange = Range("A1").Resize(5, 3) ' A1:C5
Frequently Asked Questions
How do I convert a selection to a range in VBA?
Use Set myRange = Selection after verifying that the selection is a range. You can also use Set myRange = ActiveSheet.Range(Selection.Address) to force a Range from the selected address string.
What is the difference between Range and Cells in VBA?
Range takes an address string like “A1:B2” and returns a Range object. Cells takes row and column numbers, like Cells(1,1) for A1, and also returns a Range. Cells is handy for loops, while Range is clearer for static addresses.
Can I convert a string variable to a range without using Set?
No, you must use Set to assign an object reference. Without Set, VBA tries to assign the default property (like .Value) to the variable, which causes a type mismatch error.
Why does my Range variable become Nothing?
If you use Set myRange = Range("A1") and the active sheet is not the one containing that cell, it still works. However, if you use RefersToRange on a name that refers to a constant, it returns Nothing. Always check If Not myRange Is Nothing before using it.
How do I convert a range to a string address in VBA?
Use the .Address property: Dim addr As String: addr = myRange.Address. This returns something like “$A$1:$B$2”. You can then use that string elsewhere, or convert it back to a Range with Range(addr).
Is there a way to convert a range to an array?
Yes, you can use myRange.Value to get a 2D array of values. For example: Dim arr As Variant: arr = myRange.Value. This is often faster than looping through cells, but the array is 1-based and has two dimensions even for a single row or column.
Related
- Computer Data Entry Operator Resume: Full Guide
- Changing Windows Shortcut: How to Remap Keys
- Data Entry Software for Smart Contracts: A Practical Guide
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- VBA Include Quotes in a String: Examples and FixesTo include a double quotation mark inside a VBA string, double it in the string literal: write message…
- Keyboard Macro Recorder Free? Tools, Limits and SafetyYes—free keyboard macro recorders are available, especially for Windows. Some capture keystrokes and mouse actions for replay, while…
- Excel VBA: Add Cells With Fill ColorTo insert a cell and give it a fill color with VBA, use the cell’s Insert method, then…
- Cannot Run Macro Error in Excel: Causes and SolutionsIf Excel says it cannot run a macro, first confirm that the macro exists in an open, macro-enabled…
- Excel VBA: Loop Through All Sheets in a Workbook SafelyTo loop through every worksheet in a workbook with Excel VBA, use a For Each loop over that…
- Excel VBA Error Handling: On Error, Err, and RecoveryIn Excel VBA, handle runtime errors with On Error GoTo HandlerLabel, inspect the failure through the Err object,…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.