Highlight Differences between Two Sheets, Accounting for Added Rows

michaeltsmith93

Board Regular
Joined
Sep 29, 2016
Messages
83
Hi all,

I have found a million solutions to highlight differences, but I'm having trouble finding something that accommodates the addition of a row in the middle of my data.

The idea is that I have two sheets with a few dozen columns--let's call these sheets A and B, with B being the newer version of the data. Conditional formatting is an easy solution, but it will Fill all cells below a newly added row in sheet B if I use a formula like =A1<>Sheet2!A1. If an entirely new row is added, I would like all cells in this row to be highlighted, and then I would like for it to resume the comparison, recognizing that it now needs to compare the following row to Rows(newly added row) -1, so to speak. Could someone please point me in the right direction here?

Code:
Sub Rectangle1_Click()


    Dim wsA As Worksheet
    Dim wsB As Worksheet
    Dim varA As Variant
    Dim varB As Variant
    Dim LColumn As Long
    Dim RangeToCheck As Range
    Dim iRow As Long
    Dim iCol As Long
    
    Set wsA = Worksheets(2)
    Set wsB = Worksheets(1)
    
    LColumn = wsB.Cells.Find(What:="*")
    
    RangeToCheck = Range(Cells(5, 1), Cells(1000, LColumn))
    
    varA = wsA.Range(RangeToCheck)
    varB = wsB.Range(RangeToCheck)
    
    For iRow = LBound(varA, 1) To UBound(varA, 1)
        For iCol = LBound(varA, 2) To UBound(varA, 2)
            If Not varA(iRow, iCol) = varB(iRow, iCol) Then
            
            varB(iRow, iCol).Interior.Color = 3
            
[COLOR=#ff0000]            'code to account for added row[/COLOR]
            
            End If
        Next iCol
    Next iRow


End Sub
 
Last edited:
This will create a new sheet with the names that have been removed
Code:
Sub michaeltsmith93()
   Dim Cl As Range
   Dim Ws1 As Worksheet, Ws2 As Worksheet
   
   Set Ws1 = Sheets("[COLOR=#ff0000]Pcodes[/COLOR]")
   Set Ws2 = Sheets("[COLOR=#ff0000]Postcodes[/COLOR]")
   With CreateObject("scripting.dictionary")
      .CompareMode = 1
      For Each Cl In Ws2.Range("A2", Ws2.Range("A" & Rows.Count).End(xlUp))
         .Item(Cl.Value) = Empty
      Next Cl
      For Each Cl In Ws1.Range("A2", Ws1.Range("A" & Rows.Count).End(xlUp))
         If .Exists(Cl.Value) Then .Remove Cl.Value
      Next Cl
      Sheets.Add(, Sheets(Sheets.Count)).Name = "Removed"
      Sheets("removed").Range("A2").Resize(.Count).Value = Application.Transpose(.Keys)
   End With
End Sub
Change values in red to suit
 
Upvote 0

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)

Forum statistics

Threads
1,224,824
Messages
6,181,187
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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