VBA - sheets comparison

VictorKZ

New Member
Joined
Sep 13, 2022
Messages
14
Office Version
  1. 365
Platform
  1. Windows
Hello everyone.

I need a VBA to compare two sheets and highlights the unique values.

I have this VBA, but it also highlights if a cell changed it´s position, which I don’t need. I only need the VBA to highlight which cells have different values and the unique cells in each sheets.

VBA Code:
Sub Compare_Two_Excel_Sheets()
    'Define Fields
    Dim iR As Double, iC As Double, oRw As Double
    Dim iRow_M As Double, iCol_M As Double
    Dim s1 As Worksheet, s2 As Worksheet
    Dim s3 As Worksheet
    Set s1 = ThisWorkbook.Sheets(1)
    Set s2 = ThisWorkbook.Sheets(2)
    Set s3 = ThisWorkbook.Sheets(3)
    iRow_M = s1.UsedRange.Rows.Count
    iCol_M = s1.UsedRange.Columns.Count
    For iR = 1 To iRow_M
    For iC = 1 To iCol_M
        s1.Cells(iR, iC).Interior.Color = xlNone
        s2.Cells(iR, iC).Interior.Color = xlNone
        If s1.Cells(iR, iC) <> s2.Cells(iR, iC) Then
           s1.Cells(iR, iC).Interior.Color = vbYellow
           s2.Cells(iR, iC).Interior.Color = vbYellow
           oRw = oRw + 1
           s3.Cells(oRw, 1) = s1.Cells(iR, iC)
           s3.Cells(oRw, 2) = s2.Cells(iR, iC)
        End If
    Next iC
    Next iR
End Sub

Thanks for listening.
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Cells don't change their position.

If Cell B2 contains "Any Text" in Sheet1, and nothing in Sheet2, then B2 has two different values in each Sheet, it has to be highlighted.
If Cell C2 contains nothing in Sheet1, and "Any text" in Sheet2, then C2 has two different values in each Sheet, it has to be highlighted.

Cells B2 and C2 have not changed their position in the other Sheet, each one of them has changed its value independently.

If A5 contains 10 and F16 contains 20 in Sheet1, and then A5 contains 20 and F16 contains 10 in Sheet2, it doesn't mean they have changed places.
 
Last edited:
Upvote 0
Thanks, i have an exemple:
As we can see in this imagen, it highlighted the lines 423 in both sheets, even though they are the same in values.
How can i change the VBA to not highlight cells with the same value on different lines. I need to highlight only new and unique values.
Thank you for your help.
Diagrama sem nome.drawio.png
 
Upvote 0

Forum statistics

Threads
1,214,819
Messages
6,121,741
Members
449,050
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