1
votes

I am trying to select a range in my Excel Sheet but I just can't figure out how. The data is in a way that in Row 2 all cells are filled to the end and in Column A all cells are filled to the bottom. But within the table there can be empty cells. So the bottom right corner can be empty as well. I'd like the range to start at A3.

I have tried to select the range in this way:

Range("A3",Range("ZZ2").End(xlToLeft).Range("A1000000").End(xlUp)).Select

or this way:

Range("A3",Range("A1000000").End(xlUp).Range("ZZ2").End(xlToLeft).).Select

But either way the whole table never gets selected because there is nothing in the bottom right cell. I feel like there must be a easy solution to this but I just can't see it.

enter image description here

This is a simple example of what the table could look like. The range I want to select is the green area.

2
Indeed there is an easy solution but the explanation isn't very clear. An image would help a lot in this case. - ashleedawg

2 Answers

1
votes

Look at this piece of code. It will figure out last column and last row separately - assuming that you have at least one row and one column that is filled out completely. Looking at your example data, I have taken your header row (=2) and the first column (=A)

Dim r As Range, row As Long, col As Long
With ActiveSheet
    ' Look in Col A (=1) to figure out number of rows
    row = .Cells(.Rows.Count, 1).End(xlUp).row 
    ' Look in Row 2 to figure out number of columns
    col = .Cells(2, .Columns.Count).End(xlToLeft).Column
    Set r = .Range(.Cells(3, 1), .Cells(row, col))
End With
Debug.Print r.Address
0
votes

Try,

range(cells(3, "A"), cells(2, columns.count).end(xltoleft).offset(cells(rows.count, "A").end(xlup).row - 2, 0)).select