mlam13
mlam13

Reputation: 227

Range.End() Confuzzlement

I have a general question regarding the Range.End() property in VBA. I've read up on the property here, but I'm still confused. Example:

    With ws
       lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
       MsgBox ("Last row for xlUP is: " & lastRow)
    End With

Why is the xlDirection xlUp? Why isn't it xlDown? Is it starting from the bottom row and going up then? I know this is wrong cause I used it on my code (and cause everyone uses xlUp when finding the last used row), but I don't know why. Could someone please explain?

Upvotes: 2

Views: 2322

Answers (1)

Jerry Jeremiah
Jerry Jeremiah

Reputation: 9618

End(xlDown) searches from the top down finding the last used cell before a blank cell. But that might not be the very last used cell if there are blank cells in the data. Do it your self manually: highlight a column that contains data intermixed with blanks and then press ctrl down - it will go to the cell before the first blank cell.

On the other hand End(xlUp) searches from the bottom of the range upwards for the first used cell that is before a blank cell. This will always be the last used cell in the range because we started at the bottom.

Upvotes: 6

Related Questions