Autofilter two columns with same array

vegas808

New Member
Joined
Dec 17, 2022
Messages
13
Office Version
  1. 365
Platform
  1. Windows
I can autofilter first column with an array with no problem. Trying to autofilter with two columns, in a table, using same array values. Same values in different columns are never on the same row. Tried searching around for a solution.

working if when used separately

ws.ListObjects("tblA").Range.AutoFilter Field:=2, Criteria1:=arr, Operator:=xlFilterValues
ws.ListObjects("tblA").Range.AutoFilter Field:=5, Criteria1:=arr, Operator:=xlFilterValues

Below doesn't work for me.

ws.ListObjects("tblA").Range.AutoFilter Field:=2, Criteria1:=arr, Operator:=xlOr, _
ws.ListObjects("tblA").Range.AutoFilter Field:=5, Criteria1:=arr, Operator:=xlFilterValues
 

Attachments

  • autofilter.jpg
    autofilter.jpg
    58.2 KB · Views: 5

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
You have only given us part of your code and whilst your code refers to a ListObject, your image does not really look like one as the columns don't appear to have headings.
To filter as it seems you wish, you would need to either ..
  1. Add an extra helper column to the table and AutoFilter using that or
  2. Use Advanced Filter instead of AutoFilter
I have opted for 2 above and started with this

vegas808.xlsm
ABCDEFGH
1
2Column1Column2
3A1B1
4A2B2
5A3B3
6B1A1
7B2A2
8B3A3
9C1B1
10C2B2
11C3B3
12C1
13C2
14C3
15
16
Sheet1


I then used this code ..
VBA Code:
Sub vegas808()
  Dim rCrit As Range
  Dim arrAddr As String
  
  arrAddr = Range("G3", Range("G" & Rows.Count).End(xlUp)).Address
  Set rCrit = Range("H3:H4")
  rCrit.Cells(2).Formula2 = Replace("=OR(ISNUMBER(MATCH(" & Range("tblA").Cells(1, 1).Address(0, 0) & ",#,0)),ISNUMBER(MATCH(" & Range("tblA").Cells(1, 2).Address(0, 0) & ",#,0)))", "#", arrAddr)
  Range("tblA[#All]").AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:=rCrit, Unique:=False
  rCrit.ClearContents
End Sub

.. to finish with this. Is that any use to you?

vegas808.xlsm
ABC
1
2Column1Column2
6B1A1
7B2A2
8B3A3
9C1B1
10C2B2
11C3B3
15
16
Sheet1
 
Upvote 0
Peter,

Sorry for the confusion. It was late at night and dozed off quickly soon after posting. Here's my test macro that I wanted to learn more about using autofilter so I can apply it somewhere else. Wanted to get away from For loops.

Sub autofilter2cols()

Dim ws As Worksheet: Set ws = Worksheets("Test")
Dim tblA As ListObject: Set tblA = ws.ListObjects("tblInventory")
Dim arr(), i As Variant
Dim lastrw As Integer: lastrw = ws.Range("G" & Rows.Count).End(xlUp).Row

tblA.AutoFilter.ShowAllData

'arr list will be relocated later
arr = Application.Transpose(ws.Range("G3:G" & lastrw))

For i = LBound(arr) To UBound(arr)
arr(i) = CStr(arr(i))
Next i

'Fix code here. One work but both don't
tblA.Range.AutoFilter Field:=1, Criteria1:=arr, Operator:=xlFilterValues
' tblA.Range.AutoFilter Field:=2, Criteria1:=arr, Operator:=xlFilterValues

End Sub


Your code works and even shorter. Thank you, Peter! Now I gotta look into advanced filtering. Sorry, still learning new things everyday.
 

Attachments

  • autofilter2.jpg
    autofilter2.jpg
    43.3 KB · Views: 2
Upvote 0
Peter,

I'm trying to use your code with using a Table, instead of a Range("tblA"). I'm failing miserably. Erroring out at red line

Sub vegas808()
Dim rCrit As Range
Dim arrAddr As String
Dim ws As Worksheet: Set ws = Worksheets("Test")
Dim tblA As ListObject: Set tblA = ws.ListObjects("tblInventory")

' ActiveSheet.ShowAllData
arrAddr = Range("G3", Range("G" & Rows.Count).End(xlUp)).Address
Set rCrit = Range("H3:H4")
rCrit.Cells(2).Formula2 = Replace("=OR(ISNUMBER(MATCH(" & tblA.Cells(1, 1).Address(0, 0) & ",#,0)),ISNUMBER(MATCH(" & tblA.Cells(1, 2).Address(0, 0) & ",#,0)))", "#", arrAddr)
Range("tblA[#All]").AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:=rCrit, Unique:=False
rCrit.ClearContents

ActiveSheet.ShowAllData

End Sub
 
Upvote 0

Forum statistics

Threads
1,215,069
Messages
6,122,954
Members
449,095
Latest member
nmaske

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