Professional Documents
Culture Documents
Range or a Cell as a Variable in VBA.
Range or a Cell as a Variable in VBA.
Range or a Cell as a Variable in VBA.
Excel Basics Keyboard Shortcuts Excel Tips Excel Functions Excel Formulas Advanced Excel Pivot Table
VBA PowerQuery
Contents [ hide ]
Related Tutorials
object. And just like any other variable, you can name the range variable to make it readable by the
user. To use it in a code, you need to set the range to the variable after you declare it. And for this, you
To use a range or a single cell as a variable, first, you need to declare that variable with the range data
type (object variable). Once you do that you need to specify a range of a cell to that variable using the
range object. This also gives you access to all the properties and methods that you can use with a
range.
1. First of all, use the Dim keyword and enter the name of the variable. In our example, we are
2. After that, enter the keyword, as to get the list of the data types and declare the data type as
4. Next, enter the name of the variable which you have declared in the previous line and type an
equal ( = ) to sign.
5. In the end, enter the range object and define the address of the range which you want to
1 Sub vba_range_variable()
2 Dim rng As Range
3 Set rng = Range("A1:A10")
4 End Sub
In the above code, the first line declares the variable and the second line of code, sets the range
To understand the range variable, let’s take a simple example. In VBA, when Selection property refers
to the cell or a range you have selected, that means the currently selected range.
And like the above code, when you use the keywords Set, enter the name of the variable and then use
the Selection property. The range variable rng, refers to the selected range.
Now, when you enter the name of the range variable and then enter a dot, it shows you a list of all the
properties and methods that come with a range object. See the VBA code below, where we selected
the clear method from the list to clear the selected range.
1 Sub vba_range_variable()
2 Dim rng As Range
3 Set rng = Selection
4 rng.Clear
5 End Sub
But there is one thing that you need to take care that when you are selecting an object other than a
range, in that case VBA will show you an error (VBA Type Mismatch Error).
But this is also a dynamic method where you don’t need to change the range again and again. To
create an actual dynamic range variable. you can check out the next example.
say you have declared the range A1:A10 as a variable rng , you can copy it using the following VBA
code.
Sub vba_range_variable()
Dim rng As Range
Set rng = Range("A1:A10")
rng.Copy
End Sub
You can also use a range variable to refer to a range in a function while writing a macro. Consider the
following code.
Sub vba_range_variable()
Dim iNames As Variant
Dim rng As Range
Set rng = Range("A1:A10")
Range("B1") = WorksheetFunction.Max(rng)
End Sub
It takes values from the range A1:A10 and as you have to use the variable “rng” we have used the
As I have already said, once specify a variable as a range you will also get all the properties and
methods to access. Imagine if you want to count the rows and columns. Following is the code that
could be used.
1 Sub vba_range_variable()
2 Dim rng As Range
3 Set rng = Range("A1:A10")
4 MsgBox "This range has " & _
5 rng.Rows.Count & " row(s) and " & _
6 rng.Columns.Count & " coulmn(s)."
7 End Sub
When you run this code, it shows a message with the count of rows and columns from the range that
In all the examples above, we have used range without mentioning the worksheet, so when you run
that code, VBA will always refer to the range in the active sheet. But if you want to refer to a specific
worksheet then you need to use the worksheet object along with the range.
In the above example, the range address we have used is a fixed range (A1:A10). Now there might be
a situation where you need to have a range address which requires to get update in the future. Well,
for this case, you can write a VBA code can update the range.
In the below code, we have defined three variables, one if for the range and the rest two to get the last
In the above example, the range address we have used is a fixed range (A1:A10). Now there might be
a situation where you need to have a range address which requires to get update in the future. Well,
for this case, you can write a VBA code can update the range.
In the below code, we have defined three variables, one if for the range and the rest two to get the last
Now, you need to get the last row and column with the data. This helps the code to get the updated
From here, you need to specify the range to the variable. And for this, we will use the same method
which you have learned earlier. But, to define the range you need to use the CELLS and RESIZE
property.
Set rng = Cells(1, 1).Resize(iRow, iCol)
In the above line of code, cells property tells VBA to refer to the first cell of the worksheet (i.e. A1) and
then the resize property tell VBA to increase the range of according to the row and column number
specified.
And with the iRow and iCol, you will get the number of row and column which has last cell with the
data.
What is VBA
Related Tutorials
Variable in a Message Box VBA Variables (Declare, Data Types, and Scope)
Yes Maybe No
Submit
Excel Champs is one of the top Excel blogs where you can find some of the most amazing tutorials on Excel basics,
Quick Links
Free Online Excel Courses
Excel Blog
Testimonials
Excel Guides
What is VBA
Excel Skills