VBA Run-time error 1004 when auto-filtering

stevieinselby

New Member
Joined
Nov 12, 2015
Messages
27
I have a shared workbook with frozen frames and autofilters. You know what's coming next ... yes, other people frequently leave filters set, and that messes things up when the next person comes along, takes the filters off, and now the first umpteen rows are frozen and they can't scroll. So I have put code into Workbook_Open that unfilters the page and resets the frozen frames ... all working fine. I also want to set one particular filter, and after about 100 attempts I have managed to get that working ... but only when the workbook is not shared. As soon as I share the workbook, I get the dreaded Run-time error '1004': Application-defined or object-defined error. And now because the workbook is shared, I can't even debug it to find where the error is. The worksheet is protected, I don't know if that makes a difference.

Code:
With Sheets("Sheet1")
.Activate
ActiveSheet.AutoFilter.ShowAllData
ActiveWindow.FreezePanes = False
ActiveWindow.SplitColumn = 3
ActiveWindow.SplitRow = 1
ActiveWindow.FreezePanes = True
.Range("C1:BC1").AutoFilter
.Range("C1:BC1").AutoFilter Field:=8, Criteria1:="M"
End With
If I put On Error Resume Next then it does everything in Workbook_Open except applying the filter Field8="M"

Any suggestions as to how to make it work? Or am I trying to achieve something that Excel makes impossible?
 

Some videos you may like

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

Youngdand

Board Regular
Joined
Sep 29, 2017
Messages
120
Hi,

It is more than likely the fact the worksheet is protected. you can unprotect and reprotect using the macro if you know the password using worksheet.unprotect (password) at the start of the macro.

and worksheet.protect(password) at the end.
 

Youngdand

Board Regular
Joined
Sep 29, 2017
Messages
120
Actually it looks though you also have an unnecessary step in there and instead of showing all, i just changed to disable the filter.

See below (edited to make last row dynamic)

Code:
Sub filtersheets()
Dim LR As Long
LR = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
With Sheets("Sheet1")
.Activate
.AutoFilterMode = False
ActiveWindow.FreezePanes = False
ActiveWindow.SplitColumn = 3
ActiveWindow.SplitRow = 1
ActiveWindow.FreezePanes = True
.Range("C1:BC" & LR).AutoFilter , Field:=8, Criteria1:="M"
End With
End Sub
 
Last edited:

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
38,678
Office Version
365
Platform
Windows
How about
Code:
With Sheets("Sheet2")
   .Activate
If .AutoFilterMode And .FilterMode Then .ShowAllData
   ActiveWindow.FreezePanes = False
   ActiveWindow.SplitColumn = 3
   ActiveWindow.SplitRow = 1
   ActiveWindow.FreezePanes = True
   .Range("C1:BC1").AutoFilter Field:=8, Criteria1:="M"
End With
End Sub
 

stevieinselby

New Member
Joined
Nov 12, 2015
Messages
27
That works a treat when the workbook isn't shared, but as soon as it is shared, it falls over. If I follow that line and replace ActiveSheet.AutoFilter.ShowAllData with .AutoFilterMode=False in a shared workbook then when the whirring and clanking has finished, it doesn't have AutoFilter on at all. It appears that, in a shared and protected sheet, VB can remove the AutoFilter without any trouble but can't put it back in. :sigh: :headbang:

But useful to know that there's a better syntax than the one I was using, even if it doesn't solve this particular problem!
 

Youngdand

Board Regular
Joined
Sep 29, 2017
Messages
120
Which code are you using?
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
38,678
Office Version
365
Platform
Windows
What error message did you get with my code?
 

Watch MrExcel Video

Forum statistics

Threads
1,095,630
Messages
5,445,580
Members
405,346
Latest member
Mathew Abraham

This Week's Hot Topics

Top