VBA to compare rows in two different sheets and if they match highlight in red

DMO123

Board Regular
Joined
Aug 16, 2018
Messages
99
Hi, i am new to this board. i have been searching online and cannot seem to find much help around what i am trying to do.

i am looking to compare two sheets in one workbook. for example, if sheet 1 row 1 matches a row in sheet 2 then highlight it red if not leave blank. i tried:

Sub Test_Sheet()
Dim r As Range
Dim s As Range


Set s = Sheets("Already Billed").Columns(1)


For Each r In ActiveSheet.UsedRange.Rows
If Not (s.Find(r.Cells(1, 1).Value) Is Nothing) Then
r.Interior.ColorIndex = 3
End If
Next r
End Sub

but if i change the data in column 3 it does not register as the above is only looking in column 1 but i need the whole row to be check.

can someone help

thanks :)
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Comparing an entire row is quite complex. It would be better if you had a unique value to test against. However, in order to check the whole row you could try something like this:

Code:
Sub Test_Sheet()

Dim sheetOne As Worksheet
Dim sheetTwo As Worksheet
Dim lastRow As Long
Dim thisRow As Long
Dim lastCol As Long
Dim thisCol As Long
Dim foundRow As Range
Dim lastFoundRow As Long
Dim searchRange As Range
Dim isMatch As Boolean

' Set up the sheets
Set sheetOne = ActiveSheet
Set sheetTwo = Sheets("Already Billed")

' Find the last row of the active sheet
lastRow = sheetOne.Cells(sheetOne.Rows.Count, "A").End(xlUp).Row

' Set the search range on the other sheet
Set searchRange = sheetTwo.Range("A1:A" & sheetTwo.Cells(sheetTwo.Rows.Count, "A").End(xlUp).Row)

' Look at all rows
For thisRow = 1 To lastRow
    ' Find the last column on this row
    lastCol = sheetOne.Cells(thisRow, sheetOne.Columns.Count).End(xlToLeft).Column
    
    ' Find the first match
    Set foundRow = searchRange.Find(sheetOne.Cells(thisRow, "A").Value, searchRange(searchRange.Count), xlValues, xlWhole)
    
    ' Must find something to continue
    Do While Not foundRow Is Nothing
        ' Remember the row we found it on
        lastFoundRow = foundRow.Row
        
        ' Check the found row has the same number of columns
        If sheetTwo.Cells(lastFoundRow, sheetTwo.Columns.Count).End(xlToLeft).Column = lastCol Then
            ' Assume it's a match
            isMatch = True
            
            ' Look at all the column values
            For thisCol = 1 To lastCol
                ' Compare the column values
                If sheetTwo.Cells(lastFoundRow, thisCol).Value <> sheetOne.Cells(thisRow, thisCol).Value Then
                    ' No match
                    isMatch = False
                    Exit For
                End If
            Next thisCol
            
            ' If it's still a match then highlight the row
            If isMatch Then sheetOne.Range(sheetOne.Cells(thisRow, "A"), sheetOne.Cells(thisRow, lastCol)).Interior.ColorIndex = 3
        End If
        
        ' Find the next match
        Set foundRow = searchRange.Find(sheetOne.Cells(thisRow, "A").Value, foundRow, xlValues, xlWhole)
        
        ' Quit out when we wrap around
        If foundRow.Row <= lastFoundRow Then Exit Do
    Loop
Next thisRow

End Sub

WBD
 
Upvote 0
How about
Code:
Sub CheckRows()
   Dim cl As Range
   Dim Ws1 As Worksheet
   Dim Ws2 As Worksheet
   Dim Vlu As String
   Dim Lc As Long
   
   Set Ws1 = Sheets("Sheet1")
   Set Ws2 = Sheets("sheet2")
   Lc = Ws2.Cells(1, Columns.Count).End(xlToLeft).Column
   With CreateObject("scripting.dictionary")
      For Each cl In Ws2.Range("A2", Ws2.Range("A" & Rows.Count).End(xlUp))
         Vlu = Join(Application.Index(cl.Resize(, Lc).Value, 1, 0), "|")
         .Item(Vlu) = Empty
      Next cl
      For Each cl In Ws1.Range("A2", Ws1.Range("A" & Rows.Count).End(xlUp))
         Vlu = Join(Application.Index(cl.Resize(, Lc).Value, 1, 0), "|")
         If .exists(Vlu) Then cl.Resize(, Lc).Interior.Color = vbRed
      Next cl
   End With
End Sub
 
Upvote 0
You're welcome. Fluff's code (once you adapt it) is probably faster and is a little more concise. If you run into performance issues, take a look at that.

WBD
 
Upvote 0
@wideboydixon and @Fluff if i wanted to only compare certain columns such as A - F how would i amend the above code(s)? as i know currently they are looking at the full row. as you mentioned about it is complex and i ran into problems :)

thanks!
 
Last edited:
Upvote 0
Based on Fluff's code:

Rich (BB code):
Sub CheckRows()
   Dim cl As Range
   Dim Ws1 As Worksheet
   Dim Ws2 As Worksheet
   Dim Vlu As String
   Dim Lc As Long
   
   Set Ws1 = ActiveSheet
   Set Ws2 = Sheets("Already Billed")
   Lc = 6
   With CreateObject("scripting.dictionary")
      For Each cl In Ws2.Range("A2", Ws2.Range("A" & Ws2.Rows.Count).End(xlUp))
         Vlu = Join(Application.Index(cl.Resize(, Lc).Value, 1, 0), "|")
         .Item(Vlu) = Empty
      Next cl
      For Each cl In Ws1.Range("A2", Ws1.Range("A" & Ws1.Rows.Count).End(xlUp))
         Vlu = Join(Application.Index(cl.Resize(, Lc).Value, 1, 0), "|")
         If .exists(Vlu) Then cl.Resize(, Lc).Interior.Color = vbRed
      Next cl
   End With
End Sub

The line in red indicates the number of columns to compare and highlight.

WBD
 
Upvote 0

Forum statistics

Threads
1,214,821
Messages
6,121,759
Members
449,048
Latest member
excelknuckles

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top