Comparar 2 hojas y copiar datos excel vba

[Hola Dante buenas tardes

Elaboré ésta macro para comparar 2 hojas pero sale faltando datos de la ultima comparación

Aquí la macro 

Sub Comparar_copia()
    Set h1 = Sheets("NOMBRES")
    Set h2 = Sheets("TABLA TIPOS")
    '
    u = h1.Range("B" & Rows.Count).End(xlUp).Row
    '
    h1.Range("E2:F" & Rows.Count).ClearContents
    ant = h1.Cells(u, "B")
    For i = u To 2 Step -1
    '
        Set r = h2.Columns("A")
        Set b = r.Find(h1.Cells(i, "B"), lookat:=xlWhole)
        If Not b Is Nothing Then
            f = b.Row
            If h1.Cells(i, "B") <> ant Then
                h1.Cells(i, "E") = h2.Cells(f, "B")
                h1.Cells(i, "F") = h2.Cells(f, "C")
            End If
        End If
         ant = h1.Cells(i, "B")
    Next i
    '
    MsgBox "Fin"
End Sub
Respuesta
1

Cambia esta línea

ant = h1.Cells(u, "B")

Por esta

ant = h1.Cells(u + 1, "B")


Lo que pasa es que estás asignando a ant el valor de la celda B

Y en esta línea preguntas si son diferentes

If h1.Cells(i, "B") <> ant

Pero ant y h1. Cells(i, "B") en el primer registros son iguales, es por eso que no te pone nada.

Sal u dos

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas