Autofilter method of class range failed - arrays on listbox

himjak

New Member
Joined
Jun 16, 2020
Messages
1
Office Version
  1. 2016
Platform
  1. Windows
  2. MacOS
I am new to VBA. Some help would be highly appreciated. :) I have two form control multiselect listboxes. I want to filter the source data worksheet based on selection in listbox 12 and listbox 11. The code is working fine when i multi select items from both listboxes and enter search. But I'm getting an error on array if i select either of listboxes. "Autofilter method of class range failed". Please advise


VBA Code:
Sub Button15_Click()
'search
     Dim ws As Worksheet
     Set ws = Worksheets("Action Report")
     Dim wsa As Worksheet
     Set wsa = Worksheets("PMT Landing Page")
     Dim i1 As Long
     Dim j1 As Long
     Dim i As Long
     Sheet6.Select
    
Cnt = 1
With wsa.ListBoxes("List Box 12")
        For i1 = 1 To .ListCount
            If .Selected(i1) Then
             i1 = i1 + 1
            End If
        Next i1
End With

With wsa.ListBoxes("List Box 11")
        For j1 = 1 To .ListCount
            If .Selected(j1) Then
             j1 = j1 + 1
            End If
        Next j1
End With


If i1 > 0 Then


    Dim MyArray() As String
   
    Dim arr1 As Variant
   
    Cnt = 1
   
    With wsa.ListBoxes("List Box 12")
        For i = 1 To .ListCount - 1
            If .Selected(i) Then
                    Cnt = Cnt + 1
                    ReDim Preserve MyArray(1 To Cnt)
                    MyArray(Cnt) = .List(i)
                    arr1 = MyArray(Cnt)
            End If
        Next i
    End With
   
    With ws
        .Visible = True
        .Select
        .Range("TableE").AutoFilter field:=10, Criteria1:=arr1, Operator:=xlFilterValues
    End With
 


End If



If j1 > 0 Then
   
Worksheets("PMT Landing Page").Activate
   
    Set wsa = Worksheets("PMT Landing Page")
    Cnt = 1
    Dim arr2 As Variant
   
    With wsa.ListBoxes("List Box 11")
  
        For i = 1 To .ListCount
            If .Selected(i) Then
                    Cnt = Cnt + 1
                    ReDim Preserve MyArray(1 To Cnt)
                    MyArray(Cnt) = .List(i)
                    arr2 = MyArray(Cnt)
            End If
        Next i
   
    End With

    With ws
        .Visible = True
        .Select
        .Range("TableE").AutoFilter field:=11, Criteria1:=arr2, Operator:=xlFilterValues
    End With
  
    
          
End If




End Sub
 
Last edited by a moderator:

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Hi and welcome to MrExcel.

I have Excel 2007 for windows. I see that the syntax is similar in almost everything. Maybe what you should change is the way to use the listbox.

With wsa.ListBox12 by With wsa.ListBoxes("List Box 12")

In the following way it works for me:

VBA Code:
Sub Button15_Click()
  Dim wsa As Object, cnt12 As Long, cnt11 As Long, i As Long
  Dim MyArray12(), MyArray11()
  
  Set wsa = Worksheets("PMT Landing Page")
  
  cnt12 = 0
  With wsa.ListBox12
    For i = 0 To .ListCount - 1
      If .Selected(i) Then
        ReDim Preserve MyArray12(cnt12)
        MyArray12(cnt12) = .List(i)
        cnt12 = cnt12 + 1
      End If
    Next i
  End With
  
  cnt11 = 0
  With wsa.ListBox11
    For i = 0 To .ListCount - 1
      If .Selected(i) Then
        ReDim Preserve MyArray11(cnt11)
        MyArray11(cnt11) = .List(i)
        cnt11 = cnt11 + 1
      End If
    Next i
  End With
  
  With Sheets("Action Report")
    .Range("A1").AutoFilter
    If cnt12 > 0 Then .Range("TableE").AutoFilter 10, MyArray12, xlFilterValues
    If cnt11 > 0 Then .Range("TableE").AutoFilter 11, MyArray11, xlFilterValues
  End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,213,536
Messages
6,114,208
Members
448,554
Latest member
Gleisner2

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