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?
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December

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
56,549
Office Version
  1. 365
Platform
  1. Windows

ADVERTISEMENT

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

ADVERTISEMENT

Which code are you using?
 

Fluff

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

Watch MrExcel Video

Forum statistics

Threads
1,129,553
Messages
5,636,989
Members
416,953
Latest member
broexc

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