Búsqueda de datos de Combobox al dar Enter

Tengo problemas para ejecutar una macro. Necesito que al ingresar un dato en el combobox1 y al dar Enter me devuelva en el combobox2 el resultado de la búsqueda del dato del combobox1.

Tengo esta macro, pero no se porque no funciona.

Private Sub ComboBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
If KeyCode = vbKeyReturn Then
ComboBox2.Value = WorksheetFunction.VLookup(ComboBox1.Value, ActiveSheet.Range("D3:F1000000"), 2, False)
End If
End Sub

1 Respuesta

Respuesta
1

Cambia tu evento por lo siguiente:

Private Sub ComboBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    If KeyCode = vbKeyReturn Then
        res = Application.VLookup(ComboBox1.Value, ActiveSheet.Range("D3:F1000000"), 2, False)
        If Not IsError(res) Then
            ComboBox2 = res
        Else
            MsgBox "El dato del combo1 no existe"
        End If
    End If
End Sub

Para que no te envíe error, cambia WorksheetFunction por Application.


También puedes utilizar en lugar de vlookup, el método Find:

Private Sub ComboBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    If KeyCode = vbKeyReturn Then
        Set b = Columns("D:F").Find(ComboBox1, lookat:=xlWhole)
        If Not b Is Nothing Then
            ComboBox2 = Cells(b.Row, "E")
        Else
            MsgBox "El dato del combo1 no existe"
        End If
    End If
End Sub

Saludos.Dante Amor

Recuerda valorar la respuesta.

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas