See if this works for you :
Code in the userform module:
VBA Code:
Private Sub TextboxSearch_Change()
Dim oFirstNamesRng As Range
'Sheet1 Column B (First names) <== change as required.
'Sheet1 Column C (Last names) <== change as required.
With Sheet1
Set oFirstNamesRng = Range("B1:" & .Range("B" & .Rows.Count).End(xlUp).Address)
End With
If IsError(Application.Index(oFirstNamesRng, Application.Match(TextboxSearch.Text, oFirstNamesRng, 0), 1)) = 0 Then
TextboxSearch2.Text = TextboxSearch.Text
Else
If Len(TextboxSearch2.Text) Then
TextboxSearch2.Text = ""
End If
End If
End Sub
I appreciate your support but does not work as expected. Below is the macro that works, but in case of 2 persons with same first name, it has to populate data of the 1st person even if his Last name does not match with the TextBox Search2. Your assistance is needed on "FindRow". Thanks.
Private Sub CommandSearch_Click()
Dim Firstname, LastName, AName, i As Long, FindRow As Range, CorrectDetails As Boolean
Firstname = TextBoxSearch.Value
LastName = TextBoxSearch2.Value
With Sheets("Trainees")
lrow = .Range("B" & .Rows.Count).End(xlUp).Row
For i = 7 To lrow
If Trim(.Range("B" & i).Value) = Trim(Firstname) Then
If Trim(.Range("C" & i).Value) = Trim(LastName) Then CorrectDetails = True
On Error Resume Next
Set FindRow = .Range("B:B").find(What:=Firstname, LookIn:=xlValues)
Label.Caption = FindRow.Offset(0, -1)
TextBoxFirstN.Value = FindRow.Offset.Value
TextBoxLastN.Value = FindRow.Offset(0, 1)
If FindRow.Offset(0, 2) = "Male" Then ChkBoxMale.Value = True
If FindRow.Offset(0, 2) = "Female" Then ChkBoxFemale.Value = True
TextBoxBirthTrainee.Value = FindRow.Offset(0, 3)
On Error GoTo 0
End If
Next i
If CorrectDetails = False Then
MsgBox "Record not found! Retry search." & vbCrLf & "Make sure you are searching in the right Database.", vbInformation
Exit Sub
End If
End With
End Sub