简体   繁体   中英

VBA Excel DataForm (Else If statement on search button)

i have this code that when you enter and ID and press search, it brings the data correctly. But if you want to look for the surname and by ID, it does not bring nothing. I think i done wrong the else if statement, but can´t find the error yet.

Private Sub cmdBuscar_Click()
'declarar las variables
Dim FindRow
Dim i As Integer
Dim cRow As String

'error block
On Error GoTo errHandler:

'Filtrar solo por Legajo
If Me.TextBox6 = "" Then

    'Encontrar la fila con la data
    cRow = Me.TextBox5.Value
    Set FindRow = Hoja4.Range("A:A").Find(What:=cRow, LookIn:=xlValues)

    'agregar los valores a las casillas correspondientes
    Me.TextBox7.Value = FindRow
    Me.TextBox8.Value = FindRow.Offset(0, 1)
    Me.TextBox9.Value = FindRow.Offset(0, 2)
    Me.TextBox10.Value = FindRow.Offset(0, 3)
    Me.TextBox11.Value = FindRow.Offset(0, 4)

'Filtrar solo por Apellido
Else
    If Me.TextBox6 = "" Then
        'Encontrar la fila con la data
        cRow = Me.TextBox6.Value
        Set FindRow = Hoja4.Range("B:B").Find(What:=cRow, LookIn:=xlValues)

        'agregar los valores a las casillas correspondientes
        Me.TextBox7.Value = FindRow
        Me.TextBox8.Value = FindRow.Offset(0, 1)
        Me.TextBox9.Value = FindRow.Offset(0, 2)
        Me.TextBox10.Value = FindRow.Offset(0, 3)
        Me.TextBox11.Value = FindRow.Offset(0, 4)
    End If

    'error block
    On Error GoTo 0
    Exit Sub
    errHandler:
    MsgBox "Error! Verificar los datos ingresados, porque no son correctos!" & vbCrLf & Err.Description
End If

End Sub

I assume TextBox5 is the firstName and TextBox6 is the LastName?

You just want to check if ( If ) FirstName is present if so search for that. Otherwise ( ElseIF ), check for LastName and if that is present, search for that instead? etc. Additionally, we can add a final case ( Else ) both FirstName and LastName were blank, you made a mistake, please enter one of those.

You just need one If statement with those three conditions (you had one nested in another)

Try this:

Private Sub cmdBuscar_Click()
    'declarar las variables
    Dim FindRow
    Dim i As Integer
    Dim cRow As String

    'error block
    On Error GoTo errHandler:

    'Filtrar solo por Legajo
    If Me.TextBox5 <> "" Then

        'Encontrar la fila con la data
        cRow = Me.TextBox5.Value
        Set FindRow = Hoja4.Range("A:A").Find(What:=cRow, LookIn:=xlValues)

        'agregar los valores a las casillas correspondientes
        Me.TextBox7.Value = FindRow
        Me.TextBox8.Value = FindRow.Offset(0, 1)
        Me.TextBox9.Value = FindRow.Offset(0, 2)
        Me.TextBox10.Value = FindRow.Offset(0, 3)
        Me.TextBox11.Value = FindRow.Offset(0, 4)

    'Filtrar solo por Apellido
    ElseIf Me.TextBox6 <> "" Then
        'Encontrar la fila con la data
        cRow = Me.TextBox6.Value
        Set FindRow = Hoja4.Range("B:B").Find(What:=cRow, LookIn:=xlValues)

        'agregar los valores a las casillas correspondientes
        Me.TextBox7.Value = FindRow
        Me.TextBox8.Value = FindRow.Offset(0, 1)
        Me.TextBox9.Value = FindRow.Offset(0, 2)
        Me.TextBox10.Value = FindRow.Offset(0, 3)
        Me.TextBox11.Value = FindRow.Offset(0, 4)
    Else
        MsgBox "Please enter FirstName or LastName"        
    End If

    'error block
    On Error GoTo 0
    Exit Sub
    errHandler:
    MsgBox "Error! Verificar los datos ingresados, porque no son correctos!" & vbCrLf & Err.Description

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM