Macro to Highlight all matches between two sheets

nigelse97

New Member
Joined
Nov 5, 2019
Messages
1
I'm looking to implement a macro that would first match a name in column A of Sheet1 with a name in column A of Sheet2. It would then go through every column of that row in Sheet 2 and compare it against the row in Sheet1 and highlight all the matches in Yellow. This is what I have so far but it does not seem to be running properly.

Sub RunCompare()
Call compareSheets("Sheet1", "Sheet2")
End Sub

Sub compareSheets(shtSheet1 As String, shtSheet2 As String)

Call LastCol
Dim i As Long
Dim j As Long
Dim c As Long


shtSheet1.Select
Range("A1").Select
Range(Selection, Selection.Select(xlDown)).Select
numb = Selection.Row.Count
shtSheet2.Select
Range("A1").Select
Range(Selection, Selection.Select(xlDown)).Select
numb2 = Selection.Row.Count

For i = numb To 1 Step -1
For j = numb2 To 1 Step -1
If ActiveWorkbook.Sheets(shtSheet1).Range("A" & i) = ActiveWorkbook.Sheets(shtSheet2).Range("A" & j) Then
For c = 1 To ColRow
If ActiveWorkbook.Sheets(shtSheet1).Cell(i, c) = ActiveWorkbook.Sheets(shtSheet2).Cell(j, c) Then
ActiveWorkbook.Sheets(shtSheet2).Cell(j, c).Interior.Color = vbYellow
End If
Next
End If
Next
Next

End Sub

Sub LastCol()

Dim LastCol As Integer

ColRow = ActiveSheet.UsedRange.Col.Count

End Sub
 

Some videos you may like

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.

jmacleary

Well-known Member
Joined
Oct 5, 2015
Messages
983
Office Version
2007
Platform
Windows
Hi there. I have had a quick go at this and I think this code will do it for you.
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
 

Watch MrExcel Video

Forum statistics

Threads
1,102,907
Messages
5,489,644
Members
407,703
Latest member
Chibuzo

This Week's Hot Topics

  • Timer in VBA - Stop, Start, Pause and Reset
    [CODE=vba][/CODE] Option Explicit Dim CmdStop As Boolean Dim Paused As Boolean Dim Start Dim TimerValue As Date Dim pausedTime As Date Sub...
  • how to updates multiple rows in muliselect listbox
    Hello everyone. I need help with below code. code is only chaning 1st row in mulitiselect list box. i know issue with code...
  • Delete Row from Table
    I am trying to delete a row from a table using VBA using a named range to find what I need to delete. My Range is finding the right cell. In the...
  • Assigning to a variable
    I have a for each block where I want to assign the value in column 5 of the found row to the variable Serv. [CODE=vba] For Each ws In...
  • Way to verify information
    Hi All, I don't know what to call this formula, and therefore can't search. I have a spreadsheet with information I want to reference...
  • Active Cell Address – Inactive Sheet
    How to use VBA to get the cell address of the active cell in an inactive worksheet and then place that cell address in a location on the current...
Top