VBA to AutoFilter 2 columns then select filtered rows only

dellzy

Board Regular
Joined
Apr 24, 2013
Messages
146
Hi,

I found codes by some expert here on regards to select the filtered rows excluding the headers:-

Code:
Dim aRng As Range

ActiveSheet.Range("$B$4:$U$" & Rows.Count).AutoFilter Field:=20, Criteria1:="Jan"
    
Set aRng = ActiveSheet.AutoFilter.Range.Offset(1).SpecialCells(xlCellTypeVisible).Cells(1, 1).CurrentRegion 'aRng also contains the header row
Set aRng = aRng.Offset(1, 1).Resize(aRng.Rows.Count - 1) 'and now it doesn't
aRng.Select
Selection.Copy

But got 2 situations I need help:-
1. After filtered, the selection supposed to be filtered range from column B until U, but I don't know why the coding above would select from B until V altho V doesn't have any data or header. I tried changing the integer inside bracket Offset and Cells to see the result but still not giving the output I want.

2. I need to autofilter based on 2 criterias before I select the filtered data, which is by month (Field:=20) and then by dept (Field:=10). But when I put extra autofilter code below the first one, the code to select the filtered data will prompt an error. But I still need this code in order to auto select the first filtered data which could vary based on the month and the dept.


Appreciate some help.

Thank you very much in advance.

DZ
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().

AlphaFrog

MrExcel MVP
Joined
Sep 2, 2009
Messages
16,451
Try this...

Code:
    [COLOR=darkblue]Dim[/COLOR] LastRow [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR]
    
    LastRow = Range("B:U").Find("*", , xlFormulas, , 1, 2).Row

    Range("B4:U" & LastRow).AutoFilter Field:=20, Criteria1:="Jan"
    Range("B4:U" & LastRow).AutoFilter Field:=10, Criteria1:="x"
    
    Range("B5:U" & LastRow).Copy
 

dellzy

Board Regular
Joined
Apr 24, 2013
Messages
146
Hi AlphaFrog!

Try this...

Code:
    [COLOR=darkblue]Dim[/COLOR] LastRow [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR]
    
    LastRow = Range("B:U").Find("*", , xlFormulas, , 1, 2).Row

    Range("B4:U" & LastRow).AutoFilter Field:=20, Criteria1:="Jan"
    Range("B4:U" & LastRow).AutoFilter Field:=10, Criteria1:="x"
    
    Range("B5:U" & LastRow).Copy

Wow! It works like a charm! Tqvm! :)
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,167,995
Messages
5,856,705
Members
431,828
Latest member
kARTIK12345

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
Top