Trueblue862
Board Regular
- Joined
- May 24, 2020
- Messages
- 160
- Office Version
- 365
- Platform
- Windows
Hi, I need a macro to compare the data on two sheets, sheet1 and sheet2, and then highlight any matches on sheet1. I found this piece but I'm having trouble reworking it to just highlight the matches. I'm getting some unexpected results when I try and adjust it. Any help would be appreciated.
VBA Code:
Sub RunCompare()
Call compareSheets("Sheet1", "Sheet2")
End Sub
Sub compareSheets(shtSheet1 As String, shtSheet2 As String)
Dim i As Long
Dim j As Long
Dim c As Long
Dim numb2 As Long
Dim numb As Long
Dim ColRow As Long
numb = Sheets(shtSheet1).Cells(Rows.Count, 1).End(xlUp).Row
numb2 = Sheets(shtSheet2).Cells(Rows.Count, 1).End(xlUp).Row
ColRow = Sheets(shtSheet2).UsedRange.Columns.Count
' remove the next 3 lines if you don't want to clear the backgrounds
With Sheets(shtSheet2)
.Range(.Cells(1, 1), .Cells(numb2, ColRow)).Interior.Pattern = xlNone
End With
For i = numb To 1 Step -1
For j = numb2 To 1 Step -1
If ActiveWorkbook.Sheets(shtSheet1).Range("A" & i) <> "" And ActiveWorkbook.Sheets(shtSheet1).Range("A" & i) = ActiveWorkbook.Sheets(shtSheet2).Range("A" & j) Then
For c = 1 To ColRow
If ActiveWorkbook.Sheets(shtSheet1).Cells(i, c) = ActiveWorkbook.Sheets(shtSheet2).Cells(j, c) Then
ActiveWorkbook.Sheets(shtSheet2).Cells(j, c).Interior.Color = vbYellow
End If
Next
End If
Next
Next
End Sub