Autofilter turn off question

Rockhopper3

Board Regular
Joined
Apr 11, 2006
Messages
131
Hello to all.
Have a quick one for everyone to mull over. To start, using Excel 2003 (don't say it, I already know :oops: ). I have a protected sheet with Autofilter boxes located from A4 to Y4. I need to be able to have a macro check the columns to see if any of the filters are active before saving, and if so deactivate them and go to the last cell with data in it in column A. If none of the columns have the auto filter on, just save as normal. I've worked out how to get it to work if a filter is engaged, but i get an error message if there are no filters engage and I try to save the file.
Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
lastrow = Cells(Rows.Count, "A").End(xlUp).Row
Application.ScreenUpdating = False
Application.DisplayAlerts = False
    ActiveSheet.Unprotect
    ActiveSheet.ShowAllData
    ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True _
        , AllowDeletingRows:=True, AllowSorting:=True, AllowFiltering:=True
Range("A" & lastrow).Select
Application.ScreenUpdating = True
Application.DisplayAlerts = True

The other thing is that this code will not go to the cell below the last line of data (and I have used that part of the code in many other macros). Thanks in advance to everyone for you help and quick replies with this.
Rockhopper
 

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.
Will this do?
Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    lastrow = Cells(Rows.Count, "A").End(xlUp).Row
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    ActiveSheet.Unprotect
    Range("A4").Select
    Selection.AutoFilter
    Selection.AutoFilter
    ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True _
        , AllowDeletingRows:=True, AllowSorting:=True, AllowFiltering:=True
    Range("A" & lastrow).Select
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
End Sub
 
Upvote 0
Vidar,
I just tried the code you sent. It took off the autofilter, but then moved all of the autofilter boxes from (A4:Y4) to (A1:Y1). Also it did not go to the last cell of data in column A. Thanks for your help though.
 
Last edited:
Upvote 0
So I figured it out. The problem was where I placed the select command. I've pasted the code below. Because I had the selection before the screen updating was turned back on, it would select the cell but not jump to it.
Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Application.ScreenUpdating = False
Application.DisplayAlerts = False
ActiveSheet.Unprotect
Selection.AutoFilter Field:=1
Selection.AutoFilter Field:=2
Selection.AutoFilter Field:=3
Selection.AutoFilter Field:=4
Selection.AutoFilter Field:=5
Selection.AutoFilter Field:=6
Selection.AutoFilter Field:=7
Selection.AutoFilter Field:=8
Selection.AutoFilter Field:=9
Selection.AutoFilter Field:=10
Selection.AutoFilter Field:=11
Selection.AutoFilter Field:=12
Selection.AutoFilter Field:=13
Selection.AutoFilter Field:=14
Selection.AutoFilter Field:=15
Selection.AutoFilter Field:=16
Selection.AutoFilter Field:=17
Selection.AutoFilter Field:=18
Selection.AutoFilter Field:=19
Selection.AutoFilter Field:=20
Selection.AutoFilter Field:=21
Selection.AutoFilter Field:=22
Selection.AutoFilter Field:=23
Selection.AutoFilter Field:=24
Selection.AutoFilter Field:=25
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True _
        , AllowDeletingRows:=True, AllowSorting:=True, AllowFiltering:=True
Application.ScreenUpdating = True
Application.DisplayAlerts = True
Range("B5").End(xlDown).Offset(1, -1).Select
End Sub

Thanks to everyone that hada look at this and to Vidar for trying to help me with this.
 
Upvote 0

Forum statistics

Threads
1,215,219
Messages
6,123,684
Members
449,116
Latest member
HypnoFant

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