Excel VBA Autofilter on Open

Oxalate

New Member
Joined
Apr 30, 2017
Messages
5
Dear Forum members,

I would like to add VBA code to my file which on open activates, and sets the Excel Autofilter (just the normal basic filter with arrows)on the same entire row.

The story behind is that we have a shared Excel file in our group, and sometimes other users deactivate the filter and save...or move to other rows and then save etc.
It would be nice to have the workbook on open, always filter enabled on the entire row.

So basically:
onOpen
Set Autofilter row 10.

Thank you.
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Press alt+F11 and paste this code, changing Sheet1 to the sheet you want to filter
Code:
Private Sub Workbook_Open()

            If Sheet1.AutoFilterMode = False Then
            Sheet1.Rows(10).Select
    Sheet1.Cells.AutoFilter
    End If
End Sub
 
Last edited:
Upvote 0
Thank you for your help.
This code works perfectly if there is no filter set on open, which is great.
Is it possible to adjust the code also that if the file was saved with some other row filtered, e.g. row 5, that it always filters row 10 on open by default?
 
Upvote 0
Thank you for your help.
This code works perfectly if there is no filter set on open, which is great.
Is it possible to adjust the code also that if the file was saved with some other row filtered, e.g. row 5, that it always filters row 10 on open by default?

Glad it worked. Here's modified:

Code:
Private Sub Workbook_Open()
On Error GoTo exitit:
 If Sheet1.AutoFilterMode = False Then
 Sheet1.Rows(10).AutoFilter
    Else
    'reset to row 10
    Sheet1.Cells.AutoFilter
    Sheet1.Rows(10).AutoFilter
    End If
    Exit Sub
exitit:
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,973
Messages
6,122,534
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