【问题标题】:VBA creating autofilter that contains values from arrayVBA创建包含数组值的自动过滤器
【发布时间】:2018-09-26 03:46:08
【问题描述】:

我正在尝试创建一个用户窗体来按许多条件配置自动筛选

其中之一是能够将一些单词/句子放入 TextBox(由“/”分隔)并仅显示包含其中任何一个的行。

为了实现这一点,我创建了一个表格,其中包含与 TextBox 一样多的字段包含分隔符(“/”)

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)

然后我将字符串放在一个表格中

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

最后,我使用这样的标准

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

它按预期工作,我的意思是它返回的结果与表中的任何值完全相同

无论如何,我需要能够过滤包含单元格内表格中的值的行,但它不必完全相同;只是在行中查找一些关键字

为了实现这一点,我尝试在自动过滤器中使用表格之前在表格中添加星号

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

但是,它的工作方式很奇怪。它可以找到包含“某物”+关键字+“某物”的行,但前提是: 1.我在文本框中放了一个字符串来过滤 2.我在过滤器中输入了多个字符串,但必须完全相同

例如,如果我在文本框中使用这些值:

"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)

需要明确的是,在将星星添加到表格之前它可以正常工作。虽然我给了

Ex/another/else

我可以看到单元格正好是“ex”或“another”或“else”的行

  1. 我的问题是为什么当我在字符串中使用 * 时它会停止工作?

  2. 为什么它会以这种奇怪的方式工作?

  3. 以及如何提供包含值的命名表来过滤单元格内包含任何值的行?

【问题讨论】:

标签: arrays excel vba autofilter


【解决方案1】:

这是一个类似的例子,可能会对您有所帮助。假设我们有数据,我们希望根据列 C 中的值显示行。

如果 C 单元格包含,我们希望显示该行alphabetagamma。对于一两个通配符匹配,我们可以使用 AutoFilter

如果有两个以上的条件,我们可以手动过滤。假设我们开始:

运行此代码:

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

将产生:

如果绝对需要AutoFilter,则让代码构造一个“帮助”列并在该列上进行过滤。

【讨论】:

  • 通过 .EntrieRow.Hidden 隐藏行是一个有趣的选项,但我认为它不符合我的需要。问题是工作速度。该表将有很多行,并且应该有很多参数来设置过滤器。我认为:“for i = 2”到 n 太慢了另外,我不确定如果我同时使用 .EntrieRow.Hidden 自动过滤器会如何工作。 “如果绝对需要自动筛选,则让代码构造一个“帮助”列并在该列上进行筛选。”这很有趣。通过“帮助”列,您的意思是我可以检查表格的每一行,如果该行满足条件,则输入“1”或“0”?
猜你喜欢
  • 2016-05-31
  • 1970-01-01
  • 2017-04-09
  • 2021-03-17
  • 2020-08-08
  • 2014-02-10
  • 2019-03-16
  • 2021-03-29
  • 2016-12-04
相关资源
最近更新 更多