Code for last row of data before autofilter - if necessary

bigpat

New Member
Joined
Nov 28, 2012
Messages
24
I created a macro by recording it, where I want to set an autofilter on a table that starts at row 11 and then select multiple entries.

This is the relevant part of that code and it includes cell T4806 because that is CURRENTLY the last row of data.
Code:
ActiveSheet.Range("$B$11:$T$4806").AutoFilter Field:=7, Criteria1:=Array( _
        "Breast Surgery", "Colorectal Surgery", "ENT", "General Surgery", "Oral Surgery", _
        "Paediatric Surgery", "Plastic Surgery", "Trauma And Orthopaedics", "Urology"), _
        Operator:=xlFilterValues


But the file is updated each week so next week there may be more or less data. It will always be row T though.

I searched elsewhere and came up with this next line to identify the last row of data and I know it does work. I put that line before the code above.

LR = Worksheets("MyDataSheet").Cells(Rows.Count, 2).End(xlUp).Row

But how should I change the code to incorporate LR? I tried various combinations like "$B$11:$T$"&LR&" but none work.
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
As long as you don't have any blank rows in the data you can use
Code:
ActiveSheet.Range("B11:T11").AutoFilter
Alternatively, you can use
Code:
ActiveSheet.Range("B11:T"&LR).AutoFilter
 
Upvote 0
Thank you! Do you mean that the first version is if you don't have blank rows and the second is if you do have some blanks? I tried the second version and it works fine on copies of the file with more or fewer rows but I made a copy with some blanks and it only sets the autofilter as far as the row before the first blank.

But that might be be because LR is no longer returning the right value.

I don't think there wil be any blank rows, but I'm helping my son figure this out as a favour, so it's not my data and I'm not 100% sure.
 
Upvote 0
If you use
Code:
lr = Cells.find("*", , xlValues, , xlByRows, xlPrevious, , , False).Row
This will find the last used row regardless of columns.
 
Upvote 0
I tried the second version and it works fine on copies of the file with more or fewer rows but I made a copy with some blanks and it only sets the autofilter as far as the row before the first blank....
But that might be be because LR is no longer returning the right value.

What happens if you change the LR line to
Code:
LR = Columns("B:T").Find("*", , xlValues, , xlByRows, xlPrevious).Row

Just a bit more restricted range than the code Fluff has posted in case there is data outside of the columns referenced in the filter.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,264
Members
452,627
Latest member
KitkatToby

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