Filter with VBA

elotromateo

New Member
Joined
May 19, 2010
Messages
42
I have a ListBox in a UserForm with values that I wish to use to filter a range in a worksheet. I tried using .List to create an array to filter, since that is what it uses to filter, but it gives a type mismatch. Is the .List not really an array, or is it organized differently?

Code:
ActiveSheet.Range("$A$1:$P$42").AutoFilter Field:=1, Criteria1:=GraphList.List, _
     Operator:=xlFilterValues
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
It's a 2-dimensional array, and as far as I know it's only 1-dimensional arrays you can use for the criteria in AutoFilter.

You could try assigning the list to a variable and transposing it with Application.Transpose.
 
Upvote 0
Ok, i figured it out. Just created a new array that started at 1 instead of 0 and it hasn't given me any problems.

Code:
Dim NArray() As Variant

    LenArr = 0
    
    For i = 0 To GraphList.ListCount - 1
        ReDim Preserve NArray(1 To LenArr + 1)
        LenArr = UBound(NArray) - LBound(NArray) + 1
        
        NArray(LenArr) = GraphList.List(i)
    Next
    
    
    ActiveSheet.Range("$A$1:$P$42").AutoFilter Field:=1, Criteria1:=NArray, _
        Operator:=xlFilterValues
 
Upvote 0

Forum statistics

Threads
1,224,602
Messages
6,179,843
Members
452,948
Latest member
UsmanAli786

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