I have 2 spreadsheets ( sheet 1 and Sheet 2). I wrote a macro which compares the data between two sheets and highights the difference . The macro is given below which works , but the issue is that the columns in sheet2 is changing for ex:
In Sheet 1
Name Zipcode email
In sheet 2
Name Adress Zipcode
So i want to compare zipcode with zipocde and then highlight the difference in Sheet 2 . The first column Name does not change in Sheet 1 and Sheet 2.
Below is the macro that i used.
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
In Sheet 1
Name Zipcode email
In sheet 2
Name Adress Zipcode
So i want to compare zipcode with zipocde and then highlight the difference in Sheet 2 . The first column Name does not change in Sheet 1 and Sheet 2.
Below is the macro that i used.
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