简体   繁体   中英

VBA creating autofilter that contains values from array

I am trying to create a UserForm to configure the AutoFilter by many criteria

One of them is ability to put some words/sentences in TextBox (separated by "/") and show only rows that contain any of these.

To achieve this I created a table having as many fields as TextBox contains separators ("/")

Dim SeparatorCounter As Integer
Dim Separator As String
Dim FieldNumbers As Integer
Dim BoxVal As String
BoxVal = SearchForm.SearchTextBox.Value
Separator = "/"

FieldNumbers = Len(BoxVal) - Len(Replace(BoxVal, Separator, "")) + 1
Dim FilterArray() As String
ReDim FilterArray(FieldNumbers)

Then I place the strings in a table

If FieldNumbers = 1 Then
    'If no separator, there is nothing to separate
    FilterArray(0) = BoxVal

Else
    'If there is any separator
    Dim CurrText As String
    Dim RestText As String
    RestText = BoxVal
    Dim i As Integer

    'Filed numbers = -1 because tables starts with 0
    For i = 0 To FieldNumbers - 1
        If i = FieldNumbers - 1 Then
            'If this is last part of string, there is nothing to separate
            FilterArray(i) = RestText
        Else
            'If this is not last part, cut string into 2
            CurrText = Mid(RestText, 1, InStr(1, RestText, Separator, vbTextCompare) - 1)
            RestText = Mid(RestText, 2 + Len(CurrText), 9999)
            FilterArray(i) = CurrText
        End If
    Next i
End If

And finally, I use the criteria like this

ActiveSheet.Range("B:K").Select 'Wybierz kolumny jakie będą filtrowane
Selection.AutoFilter Field:=1, Criteria1:=FilterArray, _
                Operator:=xlFilterValues

It works as expected, I mean it returns results with rows that are exactly the same as any values from the table

Anyway, I need the possibility to filter rows that contain values from the table inside cell, but it doesn't have to be exactly the same; simply looking for some keywords in rows

To achieve that I tried to add stars inside the table before using it in the AutoFilter

For i = 0 To FieldNumbers - 1
    FilterArray(i) = "*" & RTrim(LTrim(FilterArray(i))) & "*"
Next i

However, it works in weird way. It can find rows that contain "something" + keyword + "something", but only if: 1. I put a single string to filter inside textbox 2. I put more than a single string into the filter, but it has to be exactly the same

For example, if I use these values inside the textbox:

"Example" <- It works (shows rows that contain *example*)
"Ex" <- Also works 
"Ex/Ex" <- Also works
"Example/Example/Example" <- Also works
"Ex/Example" <- not working (result of AutoFilter is empty)
"Ex/anything/else" <- also not working (result of AutoFilter is empty)

To be clear, it works correct before adding stars into table. While i gave

Ex/another/else

I can see rows where cells are exactly "ex" or "another" or "else"

  1. My question is why does it stop working when I use * into strings?

  2. Why it is working in this weird way?

  3. And how I can give named table containing values to filter rows which contain any of them inside cell?

Here is a similar example that may help you. Say we have data and we want to display rows based on the values in column C .

We want to display the row if the C-cell contains either alpha or beta or gamma . For one or two wildcard matches we can use AutoFilter .

With more than two criteria, we can filter-by-hand. Say we start with:

在此处输入图片说明

Running this code:

Sub ManyWilds()
    Dim crit As String, HideRow As Boolean, N As Long
    Dim i As Long, critARR
    N = Cells(Rows.Count, "C").End(xlUp).Row

    crit = "alpha/beta/gamma"
    critARR = Split(crit, "/")

    For i = 2 To N
        HideRow = True
        v = Cells(i, "C").Value
        For Each a In critARR
            If InStr(1, v, a) > 0 Then HideRow = False
        Next a
        If HideRow Then Cells(i, 1).EntireRow.Hidden = True
    Next i
End Sub

will produce:

在此处输入图片说明

If AutoFilter is absolutely required, then have the code construct a "helper" column and filter on that column.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM