I cannot understand why this autofilter by array does not work

Comedy

New Member
Joined
Jul 26, 2010
Messages
6
I have a sub that will filter on criteria and then delete those rows, it's a sub so I can pass different filter criteria to it each time. I've decided to trim it down by passing an array instead of doing mnultiple separate filters on each row... but it fails.

I'm using excel 2007.

I've simplified the piece that fails to the below and thecode below that which works.

Sub autofilter_with_array_fails()
RemoveValue = "Array(""Value1"", ""Value2"", ""Value3"", ""Value4"", ""Value5"", ""Value6"")"
ActiveSheet.Range("A1").AutoFilter Field:=3, Criteria1:=RemoveValue, Operator:=xlFilterValues
End Sub

Sub autofilter_with_array_works()
ActiveSheet.Range("A1").AutoFilter Field:=3, Criteria1:=Array("Value1", "Value2", "Value3", "Value4", "Value5", "Value6"), Operator:=xlFilterValues
End Sub


To me they seem to be doing exactly the same thing. What am I doing wrong?
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Hi

In the second case you are using an array in Criteria1, which I think is what you want.
In the first case, you are not passing an array to Criteria1, you are using a string.

I'm not sure I understand exactly what you need, but maybe this is it?

Code:
Sub xautofilter_with_array_fails()
Dim RemoveValue As Variant

RemoveValue = Array("Value1", "Value2", "Value3", "Value4", "Value5", "Value6")
ActiveSheet.Range("A1").AutoFilter Field:=3, Criteria1:=RemoveValue, Operator:=xlFilterValues
End Sub

(which should work despite the name)
 
Upvote 0
They aren't doing the same thing.

The first code is trying to filter on the string '"Array(""Value1"", ""Value2"", ""Value3"", ""Value4"", ""Value5"", ""Value6"")"'.
 
Upvote 0

Forum statistics

Threads
1,216,100
Messages
6,128,827
Members
449,470
Latest member
Subhash Chand

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