PeterH
PeterH

Reputation: 1040

ActiveX Textbox, search for entered value

I have an ActiveX Text box linked to Cell D1.

I want to use this to search for any value that is entered into the text box.

However, if i wanted to search for Peter as soon as i type in P it begins the search, and does not let me finish typing eter

I want to be able to type, and then press ENTER to find the value.

This is what i have got to:

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

What am I doing wrong ?

Upvotes: 2

Views: 513

Answers (1)

Tim Stack
Tim Stack

Reputation: 3248

It would be wiser to use a KeyUp event, specifically for the Enter key:

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

Upvotes: 1

Related Questions