Macro de búsqueda por 2 vías

Ocupo de tu ayuda nuevamente.

Ocupo una Macro en la cual pueda llenar los datos de un cliente en una tabla sabiendo que el cliente tiene 2 códigos y que al poner un código llene el otro código desconocido y los otros datos.

Lo pongo gráficamente para explicar mejor:

1 Respuesta

Respuesta
1

Te anexo la macro para que la pongas en los eventos de la hoja llenado

Private Sub Worksheet_Change(ByVal Target As Range)
'Por.Dante Amor
    If Not Intersect(Target, Range("B:B")) Is Nothing Then
        Set h = Sheets("base")
        Set b = h.Columns("B").Find(Target.Value, lookat:=xlWhole)
        If Not b Is Nothing Then
            Application.EnableEvents = False
            Cells(Target.Row, "C") = h.Cells(b.Row, "C")
            Cells(Target.Row, "D") = h.Cells(b.Row, "D")
            Cells(Target.Row, "E") = h.Cells(b.Row, "E")
            Application.EnableEvents = True
        End If
    End If
    '
    If Not Intersect(Target, Range("C:C")) Is Nothing Then
        Set h = Sheets("base")
        Set b = h.Columns("C").Find(Target.Value, lookat:=xlWhole)
        If Not b Is Nothing Then
            Application.EnableEvents = False
            Cells(Target.Row, "B") = h.Cells(b.Row, "B")
            Cells(Target.Row, "D") = h.Cells(b.Row, "D")
            Cells(Target.Row, "E") = h.Cells(b.Row, "E")
            Application.EnableEvents = True
        End If
    End If
End Sub

Sigue las Instrucciones para poner la macro en los eventos de worksheet

  1. Abre tu libro de excel
  2. Para abrir Vba-macros y poder pegar la macro, Presiona Alt + F11
  3. Del lado izquierdo dice: VBAProject, abajo dale doble click a worksheet(tu hoja)
  4. Del lado derecho copia la macro

Gracias por la respuesta funciona al 101% de bien.

Solo como un dato adicional:  si al ingresar ya sea el código o No. Fiscal y no existe alguno de ellos, como lanzaría un mensaje de advertencia?.

Donde iría el código a agregar?

Saludos y nuevamente Gracias.

Macro actualizada:

Private Sub Worksheet_Change(ByVal Target As Range)
'Por.Dante Amor
    If Not Intersect(Target, Range("B:B")) Is Nothing Then
        Set h = Sheets("base")
        Set b = h.Columns("B").Find(Target.Value, lookat:=xlWhole)
        If Not b Is Nothing Then
            Application.EnableEvents = False
            Cells(Target.Row, "C") = h.Cells(b.Row, "C")
            Cells(Target.Row, "D") = h.Cells(b.Row, "D")
            Cells(Target.Row, "E") = h.Cells(b.Row, "E")
            Application.EnableEvents = True
        Else
            MsgBox "No existe el dato ingresado", vbExclamation
        End If
    End If
    '
    If Not Intersect(Target, Range("C:C")) Is Nothing Then
        Set h = Sheets("base")
        Set b = h.Columns("C").Find(Target.Value, lookat:=xlWhole)
        If Not b Is Nothing Then
            Application.EnableEvents = False
            Cells(Target.Row, "B") = h.Cells(b.Row, "B")
            Cells(Target.Row, "D") = h.Cells(b.Row, "D")
            Cells(Target.Row, "E") = h.Cells(b.Row, "E")
            Application.EnableEvents = True
        Else
            MsgBox "No existe el dato ingresado", vbExclamation
        End If
    End If
End Sub

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas