Macro para comparación de datos

Necesito comparar dos hojas de un libro de excel en donde estarán las siguientes columnas:

Documento de identidad, fecha y valor

La idea de la macro es que me marque en la hoja 2:

  • Los valores que estén iguales en ambas hojas en verde
  • los valores que están en la hoja 1 pero con diferencia de valor en naranja
  • los valores que no están en la hoja 1 en rojo
Respuesta
1

Esto puede aportar algo

https://youtu.be/afLArBjJyz8

1 respuesta más de otro experto

Respuesta
2

Te anexo la macro

Sub Compara_Hojas()
'Por Dante Amor
    Set h1 = Sheets("Hoja1")
    Set h2 = Sheets("Hoja2")
    h2.Columns("A:C").Interior.ColorIndex = xlNone
    For i = 2 To h2.Range("A" & Rows.Count).End(xlUp).Row
        docid = h2.Cells(i, "A").Value
        valor = h2.Cells(i, "C").Value
        Set b = h1.Columns("A").Find(docid, lookat:=xlWhole)
        If Not b Is Nothing Then
            If valor = h1.Cells(b.Row, "C").Value Then
                h2.Cells(i, "A").Interior.ColorIndex = 4    'verde
            Else
                h2.Cells(i, "A").Interior.ColorIndex = 45   'naranja
            End If
        Else
            h2.Cells(i, "A").Interior.ColorIndex = 3        'rojo
        End If
    Next
    MsgBox "Fin"
End Sub

'.[Sal u dos. Dante Amor. No olvides valorar la respuesta. 
'.[Avísame cualquier duda

Funciono perfectamente, muchas gracias.

Si ademas de marcar el documento de identidad, necesitara marcar toda la fila del registro que le debería agregar a la macro?

Adicionalmente a eso, me surgió otra necesidad. Si quisiera que esas celdas en las que los valores no son iguales (las marcadas en naranja) la macro  calcule la diferencia, y agregue ese valor  en una columna adicional, como podría hacer esto?

Muchas gracias. 

Prueba con esta

Sub Compara_Hojas()
'Por Dante Amor
    Set h1 = Sheets("Hoja1")
    Set h2 = Sheets("Hoja2")
    h2.Columns("A:C").Interior.ColorIndex = xlNone
    For i = 2 To h2.Range("A" & Rows.Count).End(xlUp).Row
        docid = h2.Cells(i, "A").Value
        valor = h2.Cells(i, "C").Value
        Set b = h1.Columns("A").Find(docid, lookat:=xlWhole)
        If Not b Is Nothing Then
            If valor = h1.Cells(b.Row, "C").Value Then
                h2.rows(i).Interior.ColorIndex = 4    'verde
            Else
                h2.rows(i).Interior.ColorIndex = 45   'naranja
                h2.cells(i, "D").value = valor - h1.Cells(b.Row, "C").Value
            End If
        Else
            h2.rows(i).Interior.ColorIndex = 3        'rojo
        End If
    Next
    MsgBox "Fin"
End Sub

[sal u dos

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas