VBA - Concatenated String in Autofilter Array not working

naj90

New Member
Joined
Feb 10, 2021
Messages
2
Office Version
  1. 2016
Platform
  1. Windows
Running the below code does not filter the actual column with Autofilter. Is there any reason why?

VBA Code:
Sub TestAutoFilter()
    'Filter based on column Importance.
    'PURPOSE: Dynamically Create Array Variable based on a Given Size
    
    Dim myArray()   As Variant
    Dim DataRange   As Range
    Dim cell        As Range
    Dim x           As Long  
    
    'Determine the data you want stored
    Set DataRange = ActiveSheet.UsedRange
    
    'Resize Array prior to loading data
    
    'Loop through each cell in Range and store value in Array
    counter = 0
    For Each chkbx In ThisWorkbook.Sheets("Sheet1").CheckBoxes
        
        If chkbx.Value > 0 And InStr(1, LCase(chkbx.Name), "check") <> 0 Then
            counter = counter + 1
        End If
    Next chkbx
    
    ReDim myArray(counter)
    For Each chkbox In ThisWorkbook.Sheets("Sheet1").CheckBoxes
        If chkbox.Value > 0 And InStr(1, LCase(chkbox.Name), "check") <> 0 Then
            myArray(x) = chkbox.Caption
            x = x + 1
        End If
    Next chkbox
    
    myArrayString = ""
    'Print values to Immediate Window (Ctrl + G to view)
    For x = LBound(myArray) To counter
        myArrayString = myArrayString & "," & myArray(x)
    Next x
    'Debug.Print (myArrayString)
    
    myRightString = Left(myArrayString, Len(myArrayString) - 1)
    myLeftString = Right(myRightString, Len(myRightString) - 1)
    'Debug.Print (RTrim(myLeftString))
    ThisWorkbook.Sheets("Data").Range("A3:F3").AutoFilter Field:=3, _
                             Criteria1:=Array(myLeftString), _
                             Operator:=xlFilterValues
    
End Sub


I also tried to change the last bit to:

Code:
Criteria1:=myArray

But nothing happens, it just keeps returning an empty filter.
 

Some videos you may like

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
36,414
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
It won't work with your concatenated string, but should work with myarray instead. If it doesn't I'd guess nothing actually matches the captions exactly - check for leading/trailing spaces for example.
 
Solution

naj90

New Member
Joined
Feb 10, 2021
Messages
2
Office Version
  1. 2016
Platform
  1. Windows
It won't work with your concatenated string, but should work with myarray instead. If it doesn't I'd guess nothing actually matches the captions exactly - check for leading/trailing spaces for example.
Thanks RoryA. Yeah, I had tried it before and it didn't work. But in subsequent tries it worked with myarray.. I have no clue why :/. But I'll leave the code here for reference for other people.
 

Watch MrExcel Video

Forum statistics

Threads
1,126,956
Messages
5,621,816
Members
415,857
Latest member
braunReivn

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
Top