Excel 2010 using vba AutoFilter to filter table by cell value returns '1004 Autofilter method of Range class failed'

eldarnegrin

New Member
Joined
Jul 9, 2013
Messages
4
Hi,
I've seen here some code samples which worked for different people, yet i cant manage to make it work.
facts:
I've checked the trust vba module in the trust center.

cell A1 contains the filter value 1 (i want to show only rows that contain 1 in column a)

table data on range("A2:D6")/table:ListObject("Table1") (I've tried both)
a b c d
1 5 9 13
2 6 10 14
3 7 11 15
4 8 12 16

the code:
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Not ActiveSheet.AutoFilterMode Then ActiveSheet.Range("A2:A6").AutoFilter [COLOR=#ff0000]'tried with ActiveSheet.AutoFilterMode=False too instead of this line[/COLOR]
    Range("A2:A6").AutoFilter Field:=1, Criteria1:=Cells(1,1) [COLOR=#ff0000]'Tried with range here too instead of Cells reference, and also simple "1" as a static criteria[/COLOR]
End Sub

Nothing seems to work.. Thanks!
 

Some videos you may like

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.

Andrew Poulsom

MrExcel MVP
Joined
Jul 21, 2002
Messages
73,092
Welcome to MrExcel.

This is what the macro recorder gave me:

Code:
ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=1, Criteria1:="1"
 

eldarnegrin

New Member
Joined
Jul 9, 2013
Messages
4
Thanks very much :)
and yes i know i also recorded it and it works when i run it,
how can i make it work automatically?

Code:
Sub Macro1()
    ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=1, Criteria1:="=" & Cells(1, 1)
End Sub
 

eldarnegrin

New Member
Joined
Jul 9, 2013
Messages
4
NM, solved it Thanks!

Wrong: Criteria1:=Cells(1,1)
Right: Criteria1:="=" & Cells(1, 1)

Thanks!
 

Watch MrExcel Video

Forum statistics

Threads
1,123,502
Messages
5,602,041
Members
414,497
Latest member
guitarmanz

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