VBA with autofilter

wisewood

Board Regular
Joined
Nov 7, 2002
Messages
193
I have a spreadsheet which uses autofilter for me to quickly analyse data.

is there any way that i can have a VBA script that will filter column A by the last value in that column?

for example - if the last 6 rows have 1191 in column A, i want to filter and show only those values.
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Like this?

Code:
Sub Test()
    Dim Crit As Long
    With Columns(1)
        Crit = .Cells(.Rows.Count, 1).End(xlUp).Value
        .AutoFilter Field:=1, Criteria1:=Crit
    End With
End Sub
 
Upvote 0
Code:
Dim vLastCellValue
vLastCellValue = Cells(Rows.Count,"A").End(xlUp).Value
Range("A1").Autofilter Fields:=1,Criteria1:=vLastCellValue
 
Upvote 0
Excellent. Thanks Andrew.

Now, I've got it activating when i do Ctrl+Shift+T (for example), if i do the same key combination again, and that filter is already in place, i want it to turn the filter off, and take me to the bottom of the list :D Any idea?
 
Upvote 0
Try:

Code:
Sub Test()
    Dim Crit As Long
    With Columns(1)
        If .Parent.AutoFilterMode = False Then
            Crit = .Cells(.Rows.Count, 1).End(xlUp).Value
            .AutoFilter Field:=1, Criteria1:=Crit
        Else
            .AutoFilter
            .Cells(.Rows.Count, 1).End(xlUp).Select
        End If
    End With
End Sub
 
Upvote 0
Awesome.

Now, i don't want the autofilter to start from row 1, i want it to start at row 9...
 
Upvote 0
Like this?

Code:
Sub Test()
    Dim Crit As Long
    With Range("A9:A" & Cells(Rows.Count, 1).End(xlUp).Row)
        If .Parent.AutoFilterMode = False Then
            Crit = .Cells(1, 1).End(xlDown).Value
            .AutoFilter Field:=1, Criteria1:=Crit
        Else
            .AutoFilter
            .Cells(1, 1).End(xlDown).Select
        End If
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,694
Members
448,979
Latest member
DET4492

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