Need some code help

Mattandy55

New Member
Joined
May 27, 2016
Messages
20
I have a dynamic range on a worksheet that starts in O2-R2. There could be hundreds of rows of data or 1 row of data (only O2-R2 populated). From there I need to copy that used range to another sheet. I am using an advanced filter so the same range O2-R2 will change for each advanced filter and paste to a different tab so the ranges changes for 4 tabs. I have a code which was working, but if one of the used ranges is only in row 2 it copies down to the last row and then when it goes to paste i get a you can't paste as the copy and paste areas aren't the same.

I currently am using:

Range("o2:R2").Select
Range(Selection, Selection.End(xlDown)).Select

which works great if my data range has more data than just O2-R2. I can use this:

Range("o2:R2").Select
Range(Selection, Selection.End(xlDown).Offset(-1)).Select

but that only works if the advanced filter criteria is only populated in O2-R2. Is there a code to do what each of them are doing but be dynamic? Any help with some code that will select the range starting O2-R2 down to the last used cells in that O-R range?

Thank you
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Try this:
VBA Code:
Sub DynamicRange()

Range("O2:R" & Cells(Rows.Count, 15).End(xlUp).Row).Select

End Sub
 
Upvote 1
Solution
If you're not sure which column in the range O:R has the last populated cell, use this:

VBA Code:
Range("O2:R" & Range("O:R").Find("*", , xlFormulas, , xlByRows, xlPrevious).Row).Select
 
Upvote 1

Forum statistics

Threads
1,214,972
Messages
6,122,530
Members
449,088
Latest member
RandomExceller01

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