Set range in VBA

lrobbo314

Well-known Member
Joined
Jul 14, 2008
Messages
3,927
Office Version
  1. 365
Platform
  1. Windows
Hello,

I use the following method to set ranges in VBA, this code will set the range = A2:Q25 with my data.

Code:
Set r = Range(Range("A2"), Range("Q" & Rows.Count).End(xlUp))

I thought that I might be able to condense this code even further but this following line of code results in setting the range to A1 only.

Code:
Set r = Range("A2:Q" & Rows.Count).End(xlUp)

I've tried searching but can't find anything about this.

I know that I can set an integer = the last row in the column, but I am tring to find a way to keep it to one line of code.

I'd just like to know for best practices sake. In the scheme of things I know that that first line is fine.

Thanks for any input.
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
I would be written like this:

Code:
Set r = Range("A2:Q" & Range("Q" & Rows.Count).End(xlUp).Row)

Which isn't really condensed any more.

Dom
 
Upvote 0
Don't think you can condense any further.

You might prefer one of the below versions though:

Code:
Set r = Range(Cells(2, 1), Cells(Rows.Count, 17).End(xlUp))
Code:
Set r = Cells(2, 1).Resize(Cells(Rows.Count, 17).End(xlUp).Row, 17)

Not really any shorter though.
 
Upvote 0
I figured as much. Just curious. Thanks for the replies though.
 
Upvote 0

Forum statistics

Threads
1,215,676
Messages
6,126,161
Members
449,295
Latest member
DSBerry

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top