Hello
I'm trying to create a macro to compare two sheets (I need to compare five but two for now is enough) and highlight the differences. The problem is that in my sheets the rows are not always in the same order and not all sheets have exactly the same number of rows so I can't use a simple a macro as the following to compare row by row because when it arrives to a row that is on a different order, it will highlight that row and the following as if they where different when is just in a different order
The data on my sheet is nothing especial, just between 1000 and 3000 rows all of them text with the same number of columns and being the first column a unique string used as ID.
I'll need something more advance that use the ID on sheet 1 to then look for that same ID on sheet 2 and finally compare those two rows. I've never programmed excel macros before and even tough I can think the logic I have no idea how to code it.
I'm trying to create a macro to compare two sheets (I need to compare five but two for now is enough) and highlight the differences. The problem is that in my sheets the rows are not always in the same order and not all sheets have exactly the same number of rows so I can't use a simple a macro as the following to compare row by row because when it arrives to a row that is on a different order, it will highlight that row and the following as if they where different when is just in a different order
Code:
Sub RunCompare()
Call compareSheets("Sheet1", "Sheet2")
End Sub
Sub compareSheets(shtSheet1 As String, shtSheet2 As String)
Dim mycell As Range
Dim mydiffs As Integer
'For each cell in sheet2 that is not the same in Sheet1, color it yellow
For Each mycell In ActiveWorkbook.Worksheets(shtSheet2).UsedRange
If Not mycell.Value = ActiveWorkbook.Worksheets(shtSheet1).Cells(mycell.Row, mycell.Column).Value Then
mycell.Interior.Color = vbYellow
mydiffs = mydiffs + 1
End If
Next
'Display a message box to demonstrate the differences
MsgBox mydiffs & " differences found", vbInformation
ActiveWorkbook.Sheets(shtSheet2).Select
End Sub
The data on my sheet is nothing especial, just between 1000 and 3000 rows all of them text with the same number of columns and being the first column a unique string used as ID.
I'll need something more advance that use the ID on sheet 1 to then look for that same ID on sheet 2 and finally compare those two rows. I've never programmed excel macros before and even tough I can think the logic I have no idea how to code it.
Last edited: