Check values form 2 text boxes

JoMali

New Member
Joined
Jun 11, 2020
Messages
3
Office Version
  1. 2010
Platform
  1. Windows
I have a sheet with multiple columns. "B" column is for Firstname and "C" column for Lastname, then a user form with two textboxes ( TextboxSearch to search Firstname and TextboxSearch2 to check Lastname).
Now I need a macro that will search the TextBoxSearch value in "B" column. If the value found, then check the TextBoxSearch2 value in the C column in the same row where the first name was found.
Can someone help me with this macro?
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
What if there are two entries in column B (Firstnames) with the same name ?
 
Upvote 0
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
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,214,784
Messages
6,121,535
Members
449,037
Latest member
tmmotairi

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top