【问题标题】:Filtering a range of data using InputBox使用 InputBox 过滤一系列数据
【发布时间】:2013-07-26 10:24:08
【问题描述】:

我需要归档一个动态表(大约 200 行/4 列)。我想使用 InputBox 以范围的形式输入过滤条件。

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

【问题讨论】:

    标签: excel vba filtering inputbox


    【解决方案1】:

    可能是的,宏记录器将能够为您完成大部分工作。启动记录器,选择您的数据,使用允许 >= 和

    然后您需要进入 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
    

    【讨论】:

      猜你喜欢
      • 2021-12-17
      • 2013-06-07
      • 1970-01-01
      • 1970-01-01
      • 2012-12-09
      • 1970-01-01
      • 1970-01-01
      • 2020-11-27
      • 1970-01-01
      相关资源
      最近更新 更多