ActiveX文本框搜索Autofilter结果非常慢 - Excel 2010

时间:2013-03-28 01:56:13

标签: excel-vba textbox activex autofilter vba

我已经设置了一个ActiveX文本框,使用以下代码过滤1,400多行自动过滤数据;

Private Sub TextBox3_Change()

Application.Calculation = xlManual

     Application.ScreenUpdating = False
Selection.AutoFilter Field:=5, Criteria1:="*" & TextBox3.Value & "*", Operator:=xlOr

On Error Resume Next

If Range("B7:B1307").SpecialCells(xlCellTypeVisible).Count = 0 Then
Call ClearAllFilters

  ActiveSheet.Range("B7:B1307").AutoFilter Field:=5, Criteria1:="<>"

 Selection.AutoFilter Field:=6, Criteria1:="*" & TextBox3.Value & "*", Operator:=xlOr
 End If
         Application.ScreenUpdating = True
 Application.Calculation = xlAutomatic
 End Sub

问题在于每次按键都在计算 - 每次按键最多需要20秒。

有没有办法阻止它做任何事情,直到用户按下回车(最好!)或焦点从文本框中取出?

如果上面的代码显示不正确,我很抱歉!它看起来不错,但预览看起来有点棘手!

谢谢, 马特

2 个答案:

答案 0 :(得分:1)

试试这个

Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, _
ByVal Shift As Integer)
    '~~> Trap enter key
    Select Case KeyCode
        Case 13
            '~~> Your code goes here
    End Select
End Sub

答案 1 :(得分:0)

在您的第二个想法中,当用户离开TextBox时,您可以使用以下事件:

Private Sub TextBox1_LostFocus()
    'your code goes here

End Sub
相关问题