Range selection VBA

Mr2017

Well-known Member
Joined
Nov 28, 2016
Messages
644
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi

I'm trying to select a range in VBA (from cell A2 to M2 down to the bottom of the range of data), but the Sub is going to the bottom of column A instead of selecting the range.

Can someone please clarify why?

I used this code:

Code:
Sub R1()


Range("A2:m2").End(xlDown).Select


End Sub
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
It sounds like you do not have any data in column A beyond row 2.
Is there some column that will ALWAYS have a data entry for all rows that contain data?
 
Upvote 0
Note that if you are just trying to select the whole range, and there are no "gaps" in your data, you could just use Current Region, i.e.
Code:
Range("A2:M2").CurrentRegion.Select
 
Upvote 0
Hi Joe

Thanks for the prompt response.

There actually is data in every row in column A.

If you look at this sample of data, which has data in cells A1:C4, the same thing happens ie the Sub goes to the end of the data in column A, instead of selecting the range A2:C4 (the Sub has been modified to go up to column C to answer your question, with a simple example)
Code:
Sub test()


Range("A2:c2").End(xlDown).Select


End Sub
[TABLE="width: 192"]
<colgroup><col width="64" span="3" style="width:48pt"> </colgroup><tbody>[TR]
[TD="class: xl63, width: 64"]1[/TD]
[TD="class: xl63, width: 64"]2[/TD]
[TD="class: xl63, width: 64"]3[/TD]
[/TR]
[TR]
[TD="class: xl63"]a[/TD]
[TD="class: xl63"]d[/TD]
[TD="class: xl63"]g[/TD]
[/TR]
[TR]
[TD="class: xl63"]b[/TD]
[TD="class: xl63"]e[/TD]
[TD="class: xl63"]h[/TD]
[/TR]
[TR]
[TD="class: xl63"]c[/TD]
[TD="class: xl63"]f[/TD]
[TD="class: xl63"]i[/TD]
[/TR]
</tbody>[/TABLE]
 
Upvote 0
Because that code is just selecting a single cell, not an entire range.
Try:
Code:
Range("A2:C" & Range("A2").End(xlDown).Row).Select
 
Upvote 0
Thanks Joe.

Apologies for the delay in responding. Some other things came up.
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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