我需要过滤一个动态表(大约200行/4列)。我想以范围的形式输入带有InputBox的筛选条件。
我需要一个按钮,抛出一个InputBox要求用户“输入序列号的范围”(例如"7-9“和"15-25"),然后过滤表。(”序列号“是表的列之一。
发布于 2013-07-26 19:53:43
这可能是肯定的,宏录制器将能够为您完成大部分工作。启动记录器,选择您的数据,使用自定义筛选器对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 Subhttps://stackoverflow.com/questions/17878916
复制相似问题