使用复选框来筛选表中的一列

huangapple go评论74阅读模式
英文:

Using checkbox to filter one column in a table

问题

I want to filter one column of a table but using checkbox in a userform, im using a "filter" command button

我想在用户窗体中使用复选框来筛选表格的一列,我正在使用一个“筛选”命令按钮

I'm using this code but i want that if there 2 or more selected checkbox both values appear on the table results

我正在使用这段代码,但我希望如果有2个或更多的复选框被选中,那么这两个值都会出现在表格结果中

Dim str As String

If EXP1.Value = True Then str = "Aftermarket"
If EXP2.Value = True Then str = "Purchasing"
If EXP3.Value = True Then str = "Quality"

ActiveSheet.ListObjects("PEP").Range.AutoFilter Field:=11, Criteria1:=Array _
(str), Operator:=xlFilterValues

Dim str As String

If EXP1.Value = True Then str = "Aftermarket"
If EXP2.Value = True Then str = "Purchasing"
If EXP3.Value = True Then str = "Quality"

ActiveSheet.ListObjects("PEP").Range.AutoFilter Field:=11, Criteria1:=Array _
        (str), Operator:=xlFilterValues
英文:

I want to filter one column of a table but using checkbox in a userform, im using a "filter" command button

I'm using this code but i want that if there 2 or more selected checkbox both values appear on the table results

Dim str As String

If EXP1.Value = True Then str = "Aftermarket"
If EXP2.Value = True Then str = "Purchasing"
If EXP3.Value = True Then str = "Quality"

  ActiveSheet.ListObjects("PEP").Range.AutoFilter Field:=11, Criteria1:=Array _
        (str), Operator:=xlFilterValues

Please help

答案1

得分: 1

你可以尝试类似这样的代码:

Dim str As String, sep As String, i As Long

    '复选框和筛选数值的配对
    arr = Array(EXP1, "Aftermarket", EXP2, "Purchasing", EXP3, "Quality")
    
    For i = LBound(arr) To UBound(arr) Step 2
        If arr(i).Value = True Then
            str = str & sep & arr(i + 1)  '添加筛选数值
            sep = "|"                     '在第一个数值后加入分隔符
        End If
    Next i
    
    '将`str`转换为筛选数组
    ActiveSheet.ListObjects("PEP").Range.AutoFilter Field:=11, _
           Criteria1:=Split(str, "|"), Operator:=xlFilterValues
英文:

You could try something like this:

Dim str As String, sep As String, i As Long
    
    'pairs of checkbox and filter values
    arr = Array(EXP1, "Aftermarket", EXP2, "Purchasing", EXP3, "Quality")
    
    For i = LBound(arr) To UBound(arr) Step 2
        If arr(i).Value = True Then
            str = str & sep & arr(i + 1)  'add the filter value
            sep = "|"                     'populate after first value
        End If
    Next i
    
    'convert `str` to an array for filtering
    ActiveSheet.ListObjects("PEP").Range.AutoFilter Field:=11, _
           Criteria1:=Split(str, "|"), Operator:=xlFilterValues

答案2

得分: 0

将所有选定的选项添加到一个字符串中,并从中创建一个数组:

Dim str As String

If EXP1.Value = True Then str = IIf(str = "", "", ";") & "Aftermarket"
If EXP2.Value = True Then str = IIf(str = "", "", ";") & "Purchasing"
If EXP3.Value = True Then str = IIf(str = "", "", ";") & "Quality"

If str <> "" Then
    ActiveSheet.ListObjects("PEP").Range.AutoFilter Field:=11, Criteria1:=Split(str, ";"), Operator:=xlFilterValues
End If
英文:

Add all selected options to a string and create an array from that:

Dim str As String

If EXP1.Value = True Then str = IIf(str=&quot;&quot;, &quot;&quot;, &quot;;&quot;) &amp; &quot;Aftermarket&quot;
If EXP2.Value = True Then str = IIf(str=&quot;&quot;, &quot;&quot;, &quot;;&quot;) &amp;&quot;Purchasing&quot;
If EXP3.Value = True Then str = IIf(str=&quot;&quot;, &quot;&quot;, &quot;;&quot;) &amp;&quot;Quality&quot;

if str &lt;&gt; &quot;&quot; Then
    ActiveSheet.ListObjects(&quot;PEP&quot;).Range.AutoFilter Field:=11, Criteria1:=split(str, &quot;;&quot;), Operator:=xlFilterValues
End If

huangapple
  • 本文由 发表于 2023年3月10日 00:56:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75687717.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定