VBA Tips: Easy way to navigating with Range Object
One of the most common use object is the Range object.
Normally, to refer a cell, we simply put the cell address into the range such as Range("A1"). Now, what is the simpliest way to refer to other cell like B1?
There are many ways to use the range object to refer to the cell from your starting cell. I find the simpliest way is the following:
For example, if I want to refer to B1, I can simply write it as such:
Range("A1")(1,2).value or range("A1")(,2).value
This will return cell B1 value.
If I want to refer to cell B2, I will write range("A1")(2,2).
This is simply an offset statement where the syntax is range()(row,column).
(1,1) refer to the start cell in the range object. In this case, it is A1. (2,2) will bring you to B2.
If you are at B2, and you type range("B2")(0,0).value , this would return the value in A1.
So, what is the point of knowing this? Well, if you are a For Loop lover like me, this will make your life sweet and easy. You could simply put your for loop counter into the row value to refer to the next row. For example,
Dim i as integer For i = 1 to 2000 msgbox range("A1")(i).address next i
Hope this help.
- JeffLo's blog
- Login or register to post comments
- 14802 reads
Recent comments
5 years 34 weeks ago
6 years 20 weeks ago
6 years 32 weeks ago
6 years 35 weeks ago
6 years 36 weeks ago
6 years 42 weeks ago
6 years 50 weeks ago
6 years 50 weeks ago
6 years 50 weeks ago
6 years 50 weeks ago