VBA to Select Dynamic Range

nc_waggoner

New Member
Joined
Sep 2, 2016
Messages
21
Good morning,

I have a workbook with 3 sheets of similar format. Each returns data starting in cell "P3:W" but can return a different number of rows on each sheet. For example, SheetA returned 5 rows so I would need to select "P3:W7" but on SheetB there is only one row so I only need to select "P3:W3". I was using the below code which worked for SheetA but selected far too many rows on SheetB:

Dim r As Long


r = Selection.End(xlDown).Row


Range("P" & Selection.Row, "W" & r).Select

2 thoughts...is there a code to select any range? Or is there a code to reference a "counter" cell (ex. Cell Z1) and select that many rows.
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
hi,

Range("p3").CurrentRegion.Select, can be used but be aware if there are row or column gaps. If there are then you will need to be a little more creative.
 
Upvote 0
There are no gaps in columns, only X amount of rows and formulas beyond that to return blank ("") values if not applicable.
 
Upvote 0
How about
Code:
Range("P3", Range("P" & Rows.Count).End(xlUp).Offset(, 7)).Select
 
Upvote 0
Fluff, this did not work. It highlighted many rows due to there being formulas in them. Is there a way to count if one of the columns (say R,S,T) is greater than 0?
 
Upvote 0
In that case how about
Code:
   Dim Lr As Long
   Lr = Range("P:P").Find("*", , xlValues, , xlbyrws, xlPrevious, , , False).Row
   Range("P3:W" & Lr).Select
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,186
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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