使用InputBox过滤一系列数据

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

我需要过滤动态表(大约200行/ 4列)。我想以输入框的形式输入范围形式的过滤条件。

我需要一个按钮,抛出一个InputBox,要求用户“输入一系列序列号”(例如“7-9”和“15-25”),然后过滤表。(“序列号”是一个列的列表。

excel vba filtering inputbox
1个回答
0
投票

可能是的,宏记录器将能够为您完成大部分工作。启动录像机,选择数据,使用自定义过滤器对id列应用自动过滤器,该过滤器允许> =和<=一个值。然后停止录音机。

然后,您需要进入VBA编辑器并修改宏以获取变量输入。

例:

宏输出

Sub Macro1()
'
' Macro1 Macro
'
    Columns("A:C").Select
    Selection.AutoFilter
    ActiveSheet.Range("$A$1:$A$53").AutoFilter Field:=1, Criteria1:=">=5", Operator:=xlAnd, Criteria2:="<=10"
End Sub

修改宏

Public Sub Macro1m()
Dim A As String
Dim B() As String
Dim Lv As Integer
Dim Hv As Integer
Dim Sv As Integer

    A = InputBox("Enter Criteria: ( [low]-[high] )") ' take your input from the user

    B = Split(A, "-") ' split the result to get the high and low values

    Lv = CInt(B(0)) ' convert the strings to integers
    Hv = CInt(B(1)) ' 

    If Lv > Hv Then ' ensure that the high value is > than low value, swapping if needed
        Sv = Hv
        Hv = Lv
        Lv = Sv
    End If

    Columns("A:C").Select ' original macro code
    Selection.AutoFilter
    ActiveSheet.Range("$A$1:$A$53").AutoFilter Field:=1, Criteria1:=">=" & Lv, Operator:=xlAnd, Criteria2:="<=" & Hv ' macro code modified to use high and low value instead of the constant 5 and 10 entered in the auto-filter configuration

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