VBA Macro - Auto Filter Multiple Criteria

natheplas

Board Regular
Joined
Aug 28, 2016
Messages
97
Hi,

I'm struggling with the below VBA. I have data In cells A6:S1176 and headings in cells A5:S5.
I want to filter column A based on the value in B1.
I then at the same time want to always filter out anything minus or below 0 in column G.
When I run this macro though I get this error message. 'Run-time error '1004, AutoFilter method of Range class failed'.
Please can someone assist? Not sure what Im doing wrong. If anything Ive asked doesn't make sense, please let me know.

Appreciate anyones help, thank you.

Sub FilterRows()
With Worksheets("Report").Range("A5:S5")
.AutoFilter field:=1, Criteria1:="B1"
.AutoFilter field:=7, Criteria1:=">0"
End With
End Sub
 
Last edited:

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
How about
Code:
Sub FilterRows()
With Worksheets("Report")
   .Range("A5:S5").AutoFilter Field:=1, Criteria1:=.Range("B1")
   .Range("A5:S5").AutoFilter Field:=7, Criteria1:=">0"
End With
End Sub
 
Upvote 0
Hi Fluff,

Thanks for coming back to me so quick.

I copied and pasted your code and I get runtime error 1004 - Autofilter method of range class failed.

What have I done wrong?
 
Upvote 0
Hi,

I'm struggling with the below VBA. I have data In cells A6:S1176 and headings in cells A5:S5.
I want to filter column A based on the value in B1.
I then at the same time want to always filter out anything minus or below 0 in column G.
When I run this macro though I get this error message. 'Run-time error '1004, AutoFilter method of Range class failed'.
Please can someone assist? Not sure what Im doing wrong. If anything Ive asked doesn't make sense, please let me know.

Appreciate anyones help, thank you.

Sub FilterRows()
With Worksheets("Report").Range("A5:S5")
.AutoFilter field:=1, Criteria1:="B1"
.AutoFilter field:=7, Criteria1:=">0"
End With

You can try the code below incase you're still getting an error.
Code:

Sub FilterRows()
Dim x As String
x = Worksheets("Report").Range("B1")

With Worksheets("Report")
.Range("A5:S5").AutoFilter Field:=1, Criteria1:=x, Operator:xlAnd
.Range("A5:S5").AutoFilter Field:=7, Criteria1:=>0, Operator:xlAnd
End With
End Sub

Using a variable helps. I hope this works.
 
Upvote 0
Is the data in a formal table?
 
Upvote 0
Hi De Bruce,

Thank you so much for trying another solution for me.

However,

.Range("A5:S5").AutoFilter Field:=1, Criteria1:=x, Operator:xlAnd
.Range("A5:S5").AutoFilter Field:=7, Criteria1:=>0, Operator:xlAnd

appear in red and I get a 'compile error - syntax error' when I try to run it.

Any ideas?



 
Upvote 0
If you click in the data & then look just above the ribbon is there something that says TABLE TOOLS & below that DESIGN
 
Upvote 0
In that case try
Code:
Sub FilterRows()
With Worksheets("Report").ListObjects("vw_StockBasic")
   .Range.AutoFilter Field:=1, Criteria1:=.Parent.Range("B1").Value
   .Range.AutoFilter Field:=7, Criteria1:=">0"
End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,942
Messages
6,122,367
Members
449,080
Latest member
Armadillos

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