通过用户输入应用的VBA多个过滤器

问题描述 投票:0回答:1

我正在尝试使用下面的数据集创建一个宏,用户可以在其中输入要过滤的国家/地区的编号,然后从列表中输入要过滤的国家/地区的名称。

[Dataset

我需要使用宏才能允许我应用多个过滤器(取决于国家/地区的数量)。到目前为止,我已经设法编写了一个代码,该代码允许我输入想要输入的尽可能多国家/地区的所有输入详细信息,但是仅显示一个正在过滤的国家/地区,如下所示。

My Attempted Result

但是如果我进入2个国家,我希望该宏理想地吐出以下结果,

Ideal Result

我使用的宏代码也可以在这里找到,

Sub autofilter()

Dim wbCopyTo As Workbook
Dim wsCopyTo As Worksheet
Dim LastRow As Long
Dim irow As Integer
Dim k As Integer
Dim n As Integer
Dim userinput(51) As Variant
'Dim userinput As Variant
Dim userinput2 As Variant
Dim storage As Variant

Set wbCopyTo = ActiveWorkbook
Set wsCopyTo = ActiveWorkbook.Worksheets("Countries")

LastRow = wsCopyTo.Cells(Rows.Count, "B").End(xlUp).Row

userinput2 = InputBox("How many Countries do you want to include?")
If StrPtr(userinput2) = 0 Then
    Exit Sub
Else
End If
For k = 1 To userinput2
    userinput(k) = InputBox("What Countries to include?")
    If StrPtr(userinput(k)) = 0 Then
        Exit For
    End If
Next k
i = 0
For ik = 1 To userinput2
    wsCopyTo.Range("$C$3:$C$" & LastRow).autofilter Field:=1, Criteria1:=Array("*" & userinput(ik) & "*", " "), Operator:=xlFilterValues
Next ik
End Sub
excel vba autofilter
1个回答
0
投票

尝试...

删除For ik循环

并替换[...]=Array("*" & userinput(ik) & "*", " "), [...]

带有[...]=userinput, [...]

© www.soinside.com 2019 - 2024. All rights reserved.