Jarom
Jarom

Reputation: 1077

Finding the first and last row numbers from a range variable

I have some code I'm working with that has excel ranges saved in variables. I need a way to find the first and the last rows of these range variables. I prefer not to use the xlend(down).row method if at all possible. I guess I could put it into an array and get the lbound and ubound values, but that seems like a lot of work for a pretty simple task. Any simple solutions to the problem?

This is what I have as an example:

sub test()
dim r as range
dim x as integer
dim y as integer

set r as worksheets("sheet1").Range("A1:A7")
x = r.row 'This gets the first row in the range.
y = r.lastrow 'this isn't valid, but I need something that will do the equivalent of this pseudo code
end sub

Upvotes: 2

Views: 4937

Answers (1)

Scott Craner
Scott Craner

Reputation: 152660

You can use this

y = r.rows.count + x -1

Upvotes: 3

Related Questions