0

I have a range that I need to select. The upper regions of the range is A2 which I can get using the following command -

Cells(2, 1)

The lower range, I can get using the following commands -

Cells(Rows.Count, 1).End(xlUp).Row
Cells(5, Columns.Count).End(xlToLeft).Column

How can I combine all my cells command into something that range will select?

I tried

range=(cells(2,1)),(Cells(Rows.Count, 1).End(xlUp).Row),(Cells(5, Columns.Count).End(xlToLeft).Column).select 

but I get a format error.

1 Answer 1

2

Remember Cells(Rows.Count, 1).End(xlUp).Row returns an integer

and

Cells(5, Columns.Count).End(xlToLeft).Column returns an integer.

So you need Cells(integer,integer).

As yours stands now it reads range(A2),integer,integer.select.

You don't use select and if you're going to use two integers, you need to wrap it in a cells(). And the whole thing in range().

 Set rng = Range(Cells(2, 1), Cells((Cells(Rows.Count, 1).End(xlUp).Row), (Cells(5, Columns.Count).End(xlToLeft).Column)))

A better way to do this would be

Sub test()
Dim rng As Range
Dim lastrow As Integer
lastrow = Cells(Rows.Count, 1).End(xlUp).Row
Dim lastcol As Integer
lastcol = Cells(5, Columns.Count).End(xlToLeft).Column

 Set rng = Range(Cells(2, 1), Cells(lastrow, lastcol))

End Sub

Now, you have no reason to select your range - just work with rng directly.

2
  • thats exactly what i was looking for. it worked perfectly. and thanks for the tutorial, that helped too.
    – DanM
    Dec 3, 2015 at 19:45
  • @DanM glad I could help. If your question is answered, click the green checkmark to mark the question as solved. Dec 3, 2015 at 19:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .