首页 > 解决方案 > ActiveX 文本框,搜索输入的值

问题描述

我有一个链接到单元格 D1 的 ActiveX 文本框。

我想用它来搜索输入到文本框中的任何值。

但是,如果我想在Peter输入后立即P搜索,它会开始搜索,并且不会让我完成输入eter

我希望能够键入,然后按 ENTER 键查找值。

这就是我必须要做的:

Private Sub TextBox1_Change()

Dim findval As String

findval = Range("D1").Value

    Cells.Find(What:=findval, After:=ActiveCell, LookIn:=xlValues, LookAt _
        :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
        False, SearchFormat:=False).Activate

End Sub

我究竟做错了什么 ?

标签: excelvbaactivex

解决方案


使用KeyUp事件会更明智,特别是Enter键:

Private Sub TextBox1_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    Dim findval As String
    If KeyCode = vbKeyReturn Then
        findval = Range("D1").Value

        Cells.Find(What:=findval, After:=ActiveCell, LookIn:=xlValues, LookAt _
        :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
        False, SearchFormat:=False).Activate
    End If
End Sub

推荐阅读