VBA to compare rows in two different sheets, if match highlight cell

JeremyG12

New Member
Joined
Feb 13, 2024
Messages
9
Office Version
  1. 365
Platform
  1. Windows
Hello. New to this board. I found some code that compares two worksheets (sheet 2 with sheet 1) to determine if there are any differences in the rows. The code works perfect for highlighting the entire row. If there is a difference in the row on sheet 2, it is highlighted in red. Instead of highlighting the entire row, I would like to highlight the cell(s) where a change was found in each row.

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 Ws1.Range("A2", Ws1.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 Ws2.Range("A2", Ws2.Range("A" & rows.Count).End(xlUp))
Vlu = Join(Application.index(cl.Resize(, Lc).Value, 1, 0), "|")
If .exists(Vlu) = False Then cl.Resize(, Lc).Interior.color = RGB(250, 0, 0)
Next cl
End With
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Sorry, here's the code.

Excel Formula:
Lc = Ws2.Cells(1, Columns.Count).End(xlToLeft).Column
With CreateObject("scripting.dictionary")
    For Each cl In Ws1.Range("A2", Ws1.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 Ws2.Range("A2", Ws2.Range("A" & rows.Count).End(xlUp))
        Vlu = Join(Application.index(cl.Resize(, Lc).Value, 1, 0), "|")
        If .exists(Vlu) = False Then cl.Resize(, Lc).Interior.color = RGB(250, 0, 0)
    Next cl
End With
I have a question. I am trying this particular code but getting a variable undefined error. What type of variable declaration is used for Lc here? Thanks.
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,183
Members
452,615
Latest member
bogeys2birdies

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