You should start of by removing this line of code.
NameText.Value = NameText.Value + Chr(KeyAscii)
That's basically adding the character you've just typed into the textbox to what's already in the textbox, so you end up with a duplicate.
This also needs to go I think
That's moving the cursor back to the start of the textbox so the next character you press will appear before the previous character(s).
Not 100% sure what this is doing but I don't think it's needed.
If Len(NameText.Value) > 1 Then
If Mid(NameText.Value, Len(NameText.Value), 1) = Mid(NameText.Value, Len(NameText.Value) - 1, 1) Then
NameText.Value = Left$(NameText.Value, Len(NameText.Value) - 1)
End If
End If
After making those changes the KeyPress event would look like this.
Private Sub NameText_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Select Case KeyAscii
Case Asc("a") To Asc("z")
Case Asc("A") To Asc("Z")
Case Asc(",")
Case Else
KeyAscii = 0
End Select
CurrentLength = Len(NameText.Value)
If CurrentLength > 0 Then
SearchText = NameText.Value
With Sheets("Enrollment")
Location = 1
lastRw = .Range("A" & .Rows.Count).End(xlUp).Row
StringFound = False
Do While StringFound = False And Location < lastRw + 1
Location = Location + 1
CurrentNameString = Left$(Range("C" & Location).Value, CurrentLength)
If CurrentNameString = SearchText Then
UserForm2.Label5.Caption = Range("C" & Location).Value
UserForm2.Label4.Caption = Range("A" & Location).Value
StringFound = True
EmployeeFound = True
End If
Loop
If Location > lastRw Then
UserForm2.Label4.ForeColor = &HFF&
UserForm2.Label5.Caption = "Not Found"
EmployeeFound = False
End If
End With
End If
End Sub
Bookmarks