我想制作一个自动过滤器来搜索给定的年龄间隔。我已经输入了两个文本框(ActiveX),在第一个文本框中,您应该能够写出年龄下限,而在第二个文本框中,可以写出年龄上限。
代码看起来像这样(当我没有Textbox2.value作为上限时,代码运行完美)
Private Sub TextBox1_Change()
If IsNumeric(TextBox1) And IsNumeric(TextBox2) Then
Sheets("data").Select
ActiveSheet.Range("$A$1:$Z$1000").AutoFilter Field:=27, Criteria1:=">=" & TextBox1.Value, Operator:=xlAnd, Criteria2:="<=" & TextBox2.Value
Sheets("filter").Select
End If
End Sub
我也尝试过以just开头;这似乎也不起作用。
If IsNumeric(TextBox1) Then
有任何想法或可能更好的方法吗?
filter
工作表代码。调整满足您需求的常量。完成后删除Debug.Print
行测试。Select
和Activate
。The代码
Option Explicit
Private Sub TextBox1_Change()
TextBoxChange
End Sub
Private Sub TextBox2_Change()
TextBoxChange
End Sub
Sub TextBoxChange()
Const filterSheetName As String = "data"
Const filterRangeAddress As String = "A1:E10"
Const filterField As Long = 5
Debug.Print "Before: " & TextBox1.Value, TextBox2.Value
If IsNumeric(TextBox1) And IsNumeric(TextBox2) Then
Debug.Print "Between: " & TextBox1.Value, TextBox2.Value
If TextBox1.Value <= TextBox2.Value Then
Debug.Print "After: " & TextBox1.Value, TextBox2.Value
ThisWorkbook.Worksheets(filterSheetName) _
.Range(filterRangeAddress).AutoFilter _
Field:=filterField, Criteria1:=">=" & TextBox1.Value, _
Operator:=xlAnd, Criteria2:="<=" & TextBox2.Value
End If
End If
End Sub