Run time error '1004': Autofilter method of Range Class failed

StaffCPP

New Member
Joined
Aug 5, 2010
Messages
19
I am using Excel 2007. I created a macro today to hide all the rows in a spreadsheet that contain a zero value in them. However, the first time I run the macro I get this run time error. If I click "end" and then push the macro button again, it runs fine - no debugging necessary. Any ideas how I get the error message to stop popping up? My macro is as follows (and yes, it is filtering through over 5000 rows):

Sub AutoHide()
'
' AutoHide Macro
' Automatically hide rows with a 0 value.
'
' Keyboard Shortcut: Ctrl+h
'
Selection.AutoFilter
ActiveSheet.Range("$A$3:$J$5071").AutoFilter Field:=10, Criteria1:=">0", _
Operator:=xlAnd

End Sub

The section in red is what it is hilighting telling me is what is wrong when I click on debug. Again, I don't have to make any changes to anything, just go back to the button and click it again, and it runs fine. Does it matter that I made my spreadsheet a template? It seems like that is when I started getting this error, but I could be wrong.

Thank you!
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Is the filter set up already when you run it? If so you need to get rid of this line:

Selection.AutoFilter

If not after you run it one time you need to get rid of that line. That is turning the filter on and off and causing this error.

Maybe you can run this as a test:

Rich (BB code):
Sub AutoHide()
'
' AutoHide Macro
' Automatically hide rows with a 0 value.
'
' Keyboard Shortcut: Ctrl+h
'
if not activesheet.autofiltermode then ActiveSheet.Range("$A$3:$J$5071").AutoFilter
ActiveSheet.Range("$A$3:$J$5071").AutoFilter Field:=10, Criteria1:=">0", _
Operator:=xlAnd
End Sub
Hope that helps.
 
Upvote 0
The filter is not selected yet when I start the macro. The macro selects filter and then selects the range and criteria. This error only happens the very first time I run the macro each time I open the workbook.
 
Upvote 0
So does my code not work? When your code is setting up the filter is it setting it to the right range?
 
Upvote 0
I apologize. Tired eyes looking at everything yesterday, I couldn't actually see what was different. Fresh eyes this morning and everything is clear! Your macro does work wonderfully. Thank you so much!!
 
Upvote 0

Forum statistics

Threads
1,213,560
Messages
6,114,304
Members
448,564
Latest member
ED38

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