我有这个代码,当您输入 ID 并按搜索时,它会正确显示数据。但是如果你想通过姓氏和身份证来寻找,它不会带来任何东西。我认为我做错了 else if 语句,但还找不到错误。
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
最佳答案
我假设 TextBox5 是 firstName 而 TextBox6 是 LastName?
您只想检查 ( If ) FirstName 是否存在,如果存在则搜索。否则( ElseIF ),检查姓氏,如果存在,则改为搜索?等等。此外,我们可以添加最后一个案例( Else )FirstName 和 LastName 都是空白的,你犯了一个错误,请输入其中一个。
您只需要一个具有这三个条件的 If 语句(您有一个嵌套在另一个条件中)
尝试这个:
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
关于excel - VBA Excel DataForm(搜索按钮上的 Else If 语句),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60058809/