首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用InputBox过滤一定范围的数据

使用InputBox过滤一定范围的数据
EN

Stack Overflow用户
提问于 2013-07-26 18:24:08
回答 1查看 12.6K关注 0票数 0

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

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

EN

回答 1

Stack Overflow用户

发布于 2013-07-26 19:53:43

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

然后,您只需进入VBA编辑器并修改宏以接受变量输入。

示例:

宏输出

代码语言:javascript
运行
复制
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

修改的宏

代码语言:javascript
运行
复制
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
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17878916

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档