In VBA, As Range declares a variable that holds a reference to a cell or a group of cells, and you use it to write cleaner, faster, and more flexible macros than hard-coding ranges like Range("A1") every time. For example, Dim rng As Range creates a variable you can assign any range to, then manipulate with methods like .Value or .Select. This article explains what As Range means, how to declare and use it, common mistakes, and practical examples you can apply immediately.
What Does “As Range” Mean in VBA?
In VBA, every variable must have a data type, and Range is an object data type that represents one or more cells in a worksheet. When you write Dim myRange As Range, you are telling VBA that myRange will point to a Range object. This is different from a simple variable like Dim x As Integer, which stores a number. Instead, myRange stores a reference—like a pointer—to a specific range of cells.
Why use it? Because it makes your code more readable and maintainable. Instead of repeating Range("B2:D10") in every line, you assign it once to a variable and then use the variable. It also allows you to pass ranges to functions and subroutines, making your code modular and reusable.
How to Declare and Set a Range Variable
Declaring a range variable is simple: use the Dim statement. But a declaration alone doesn’t point to any cells; you must use Set to assign a range to it. This is a common point of confusion—if you forget Set, you’ll get a runtime error.
Here’s the correct syntax:
Dim rng As Range
Set rng = Range("A1:C10")
You can also set it to a named range, a selection, or a range from another worksheet:
Set rng = Worksheets("Sheet1").Range("A1")Set rng = Selection(the currently selected cells)Set rng = Range("NamedRange")
Once set, you can use rng anywhere you would use a range literal. For example:
rng.Value = 100
rng.Font.Bold = True
Using Range Variables in Loops and Operations
Range variables shine when you need to loop through cells or perform the same operation on multiple ranges. Instead of writing a loop that references Range("A1"), Range("A2"), etc., you can loop through a range variable’s cells.
Example: Sum values in a range and display the result.
Dim rng As Range
Dim cell As Range
Dim total As Double
Set rng = Range("A1:A10")
For Each cell In rng
total = total + cell.Value
Next cell
MsgBox total
You can also use range variables with methods like .Copy or .Delete, and they work with Excel’s built-in functions via Application.WorksheetFunction.
Common Mistakes and How to Avoid Them
One of the most frequent errors is forgetting the Set keyword. If you write rng = Range("A1") (without Set), VBA will try to assign the default property (which is .Value) to a variable, causing a type mismatch error. Always use Set when assigning an object.
Another mistake is assuming a range variable automatically updates when the underlying cells change. A range variable is a snapshot reference—if you insert or delete rows, the range may shift or expand depending on how it was defined. For example, Set rng = Range("A1:A10") will always refer to those exact cells, even if you insert a row above; it won’t automatically include the new row. To make a dynamic range, use CurrentRegion or End(xlDown).
Also, be careful when using Select or Activate—these are slow and often unnecessary. Instead of selecting a range to change its value, just use rng.Value = ... directly.
Range Variables vs. Other Ways to Reference Cells
You might wonder why not just use Cells(row, column) or Range("A1") directly. The advantage of a range variable is reusability and clarity. Compare:
' Without variable
Range("A1:A10").Value = 5
Range("A1:A10").Font.Bold = True
Range("A1:A10").Interior.Color = vbYellow
' With variable
Dim rng As Range
Set rng = Range("A1:A10")
rng.Value = 5
rng.Font.Bold = True
rng.Interior.Color = vbYellow
The second version is easier to read and modify—if you need to change the range, you only change one line. It also reduces the chance of typos.
Practical Examples: Working with Range Variables
Here are a few common scenarios where As Range is particularly useful.
Example 1: Sum a dynamic range
Dim rng As Range
Set rng = Range("A1", Range("A1").End(xlDown)) ' From A1 down to last non-empty cell
MsgBox Application.WorksheetFunction.Sum(rng)
Example 2: Pass a range to a subroutine
Sub ProcessRange(rng As Range)
rng.Font.Color = vbRed
End Sub
Sub Caller()
Dim myRange As Range
Set myRange = Range("B2:B5")
ProcessRange myRange
End Sub
Example 3: Loop through columns and rows
Dim rng As Range
Dim row As Range
Dim col As Range
Set rng = Range("A1:C3")
For Each row In rng.Rows
For Each col In row.Columns
col.Value = col.Row * col.Column
Next col
Next row
Performance and Best Practices
Using range variables can improve performance, especially when you avoid Select and Activate. These methods force Excel to update the user interface, slowing down your macro. Instead, work directly with the range object.
Another best practice is to use Option Explicit at the top of your module to force variable declaration, which catches typos and undeclared variables. Also, always declare the range variable as close to its use as possible, and set it to Nothing when you’re done if it’s a large object—though VBA handles this automatically in most cases.
If you’re working with very large ranges (thousands of rows), consider reading the values into an array first, processing the array, then writing back. This is much faster than accessing each cell individually.
Frequently Asked Questions
Do I need Set when declaring a range variable? Yes, always use Set when assigning a range to a variable. Without it, VBA tries to assign the default property (usually .Value), which causes an error if the range is multi-cell.
Can I use As Range in a function argument? Absolutely. You can declare a parameter as rng As Range in a Function or Sub, and then pass any range when calling it. This makes your code modular.
How do I make a range variable that automatically expands? Use Range("A1").CurrentRegion to get the contiguous block of data around A1, or use Range("A1", Range("A1").End(xlDown)) to go down until an empty cell. These are dynamic references.
What is the difference between Range and Cells? Range refers to a specific block (e.g., Range("A1:B2")), while Cells refers to a single cell by row and column index (e.g., Cells(1,1) is A1). You can combine them: Range(Cells(1,1), Cells(2,2)).
Why do I get a “Type Mismatch” error when using As Range? This usually happens because you forgot Set, or you’re trying to assign a value to a range variable. Remember: Set for objects, = for values.
Conclusion
Mastering As Range is a fundamental step in writing effective VBA code. It allows you to write cleaner, more flexible macros, avoid repetitive code, and improve performance by avoiding Select. Start by declaring a range variable in your next macro, and you’ll quickly see the benefits. Remember to always use Set, and save a copy of your workbook before running any macro, as VBA actions cannot be undone with Ctrl+Z.
Related
- Generate 4 Random Numbers in Python: Easy Steps
- Free Time Tracking Excel Template: How to Build One
- Free Student Attendance Template Excel: Get One
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- Active Range in VBA: What It Means and How to Use ItIn Excel VBA, “active range” usually means the cell or cells currently selected, but ActiveRange is not a…
- Excel VBA Sub or Function Not Defined? Fix It“Sub or Function not defined” is a VBA compile error: Excel cannot resolve the name of a procedure…
- Excel VBA Mid Function: Syntax, Examples, and TipsIn Excel VBA, the Mid function returns part of a string, starting at a position you specify. Use…
- VBA Send Email to Multiple RecipientsYes. In Excel VBA on Windows, you can create an Outlook email and add several addresses to its…
- VBA Range Array: Read and Write Cells EfficientlyA VBA range array is a Variant array that holds values from a worksheet range in memory, so…
- Excel VBA AutoFilter Last Row: Find the Right RangeTo include every record in an Excel VBA AutoFilter, find the last data row and build the filter…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.