Search code examples
excelvbauserform

Userform to search for two criteria, then paste row's data to userform textboxes


I am getting a run-time error '13': type mismatch for one of the lines marked below. I want to be able to have a userform that you can type two criteria into, then it will search for the row that has both of those criteria and paste the corresponding cells' values to the 11 userform textboxes. I'm not sure why it is giving me an error for this line, or if there is a better way to do this.

Private Sub CommandButton1_Click()

    txt1.Visible = True
    txt2.Visible = True
    txt3.Visible = True
    txt4.Visible = True
    txt5.Visible = True
    txt6.Visible = True
    txt7.Visible = True
    txt8.Visible = True
    txt9.Visible = True
    txt10.Visible = True
    txt11.Visible = True

    Dim ws As Worksheet
    Set ws = Sheets("The Goods")
    ws.Activate

    Dim SearchSearch As Variant
    SearchSearch = txtsearch.Value
    Dim SearchName As Variant
    SearchName = txtname.Value


        If Trim(txtsearch.Value) = "" Then
        MsgBox "Search can't be left blank.", vbOKOnly + vbInformation, "Search"
        End If
        If Trim(txtname.Value) = "" Then
        MsgBox "Name can't be left blank.", vbOKOnly + vbInformation, "Name"
        End If

    Dim FirstAddress As String, cF As Range

    With ThisWorkbook.Sheets("The Goods").Range("D:D") 'txtsearch will be in the range D:D

        Set cF = .Find(What:=SearchSearch, _
                       after:=ActiveCell, _
                       LookIn:=xlValues, _
                       LookAt:=xlPart, _
                       SearchOrder:=xlByColumns, _
                       SearchDirection:=xlNext, _
                       MatchCase:=False, _
                       SearchFormat:=False) ' line that is giving me an error

     With ThisWorkbook.Sheets("The Goods").Range("B:B") 'txtname will be in the range B:B

        Set cF = .Find(What:=SearchName, _
                       after:=ActiveCell, _
                       LookIn:=xlValues, _
                       LookAt:=xlPart, _
                       SearchOrder:=xlByColumns, _
                       SearchDirection:=xlNext, _
                       MatchCase:=False, _
                       SearchFormat:=False)

        txt1.Value = cF.(0, 5).Value 
        txt2.Value = cF(0, 3).Value 
        txt3.Value = cF(0, 6).Value 
        txt4.Value = cF(0, 7).Value 
        txt5.Value = cF(0, 8).Value 
        txt6.Value = cF(0, 9).Value 
        txt7.Value = cF(0, 10).Value 
        txt8.Value = cF(0, 11).Value 
        txt9.Value = cF(0, 12).Value
        txt10.Value = cF(0, 13).Value 
        txt11.Value = cF(0, 14).Value 

    End With
End With

End Sub

Private Sub CommandButton3_Click()
Dim iExit As VbMsgBoxResult
iExit = MsgBox("Are you sure you want to exit?", vbQuestion + vbYesNo, "Search System")

If iExit = vbYes Then
Unload Me
End If
End Sub

Solution

  • The code below is a simple For Loop, which loops through each cel in Column B and checks for txtname.Value, and using offset to check if Column D value is equal to txtsearch.Value. If both match, then it will write the values for that row into the userform text boxes. You can change the TextBox1 to txt1, etc.

    Private Sub CommandButton1_Click()
    Dim ws As Worksheet, cel As Range
    
    Set ws = Sheets("The Goods")
    
    
        For Each cel In ws.Cells(2, 2).Resize(ws.Cells(Rows.Count, 2).End(xlUp).Row).Cells
    
            If cel.Value = Me.txtname.Value And cel.Offset(, 2).Value = Me.txtsearch.Value Then
    
                Me.TextBox1.Value = cel.Offset(, 3).Value 'Change to your textbox naming scheme
                Me.TextBox2.Value = cel.Offset(, 1).Value
                Me.TextBox3.Value = cel.Offset(, 4).Value
                Me.TextBox4.Value = cel.Offset(, 5).Value
                Me.TextBox5.Value = cel.Offset(, 6).Value
                Me.TextBox6.Value = cel.Offset(, 7).Value
                Me.TextBox7.Value = cel.Offset(, 8).Value
                Me.TextBox8.Value = cel.Offset(, 9).Value
                Me.TextBox9.Value = cel.Offset(, 10).Value
                Me.TextBox10.Value = cel.Offset(, 11).Value
                Me.TextBox11.Value = cel.Offset(, 12).Value
    
            End If
    
        Next cel
    End Sub