highlight different data between two sheets with another and copy to a new sheet

KalilMe

Active Member
Joined
Mar 5, 2021
Messages
399
Office Version
  1. 2016
Platform
  1. Windows
hi
I have two sheets .it should compare with third sheet and should highlight the data are xisted in two sheets(first,second) but they ar not existed in sheet third and copy the highlighted data in a new sheet as fourth sheet

first
PDF2Excel.xls
ABCDE
1CODECOPRORQT
2AAkm100MM1NN1123
3BBkm101MM2NN2124
4CCkm102MM3NN3125
5DDkm103MM4NN4126
6EEkm104MM5NN5127
7FFkm105MM6NN6128
8GGkm106MM7NN7129
9HHkm107MM8NN8130
10JJkm108MM9NN9131
Main Sheet



second
PDF2Excel.xls
ABCDE
1CODECOPRORQ1
2AAkm100MM1NN1123
3BBkm101MM2NN2124
4CCkm102MM3NN3125
5DDkm103MM4NN4126
6EEkm104MM5NN5127
7FFkm105MM6NN6128
8GGkm106MM7NN7129
9NNkm107MM8NN8130
10KKkm108MM9NN9131
Page-1



third
PDF2Excel.xls
ABCDEF
1CODECOPRORQTQ1
2AAkm100MM1NN1123123
3BBkm101MM2NN2124124
4CCkm102MM3NN3125125
5DDkm103MM4NN4126126
6EEkm104MM5NN5127127
7FFkm105MM6NN6128128
8GGkm106MM7NN7129129
Page-2


fourth
PDF2Excel.xls
ABCDEF
1CODECOPRORQTQ1
2HHkm107MM8NN8130-
3JJkm108MM9NN9131-
4NNkm107MM8NN8-130
5KKkm108MM9NN9-131
RE

thanks
 
Try this:

VBA Code:
Sub highlight_different_data()
  Dim sh1 As Worksheet, sh2 As Worksheet, sh3 As Worksheet, sh4 As Worksheet
  Dim dic As Object
  Dim i As Long, lr As Long
  
  Application.ScreenUpdating = False
  Set sh1 = Sheets("Main sheet")  'first
  Set sh2 = Sheets("Page-1")      'second
  Set sh3 = Sheets("Page-2")      'third
  Set sh4 = Sheets("RE")          'fourth
  Set dic = CreateObject("Scripting.Dictionary")
  sh4.Rows("2:" & Rows.Count).ClearContents
  
  For i = 2 To sh3.Range("A" & Rows.Count).End(3).Row
    dic(sh3.Range("A" & i).Value) = Empty
  Next
  
  For i = 2 To sh1.Range("A" & Rows.Count).End(3).Row
    If Not dic.exists(sh1.Range("A" & i).Value) Then
      With sh1.Range("A" & i).Resize(1, 5)
        .Interior.Color = vbRed
        lr = sh4.Range("A" & Rows.Count).End(3)(2).Row
        sh4.Range("A" & lr).Resize(1, 5).Value = .Value
        sh4.Range("F" & lr).Value = "-"
      End With
    End If
  Next
  For i = 2 To sh2.Range("A" & Rows.Count).End(3).Row
    If Not dic.exists(sh2.Range("A" & i).Value) Then
      With sh2.Range("A" & i).Resize(1, 5)
        .Interior.Color = vbRed
        lr = sh4.Range("A" & Rows.Count).End(3)(2).Row
        sh4.Range("A" & lr).Resize(1, 5).Value = .Value
        sh4.Range("E" & lr).Value = "-"
        sh4.Range("F" & lr).Value = sh2.Range("E" & i).Value
      End With
    End If
  Next
End Sub
 
Upvote 0
perfect ! but I have last thing when change the data in sheet Page-2 and become matched with the two sheets . it supposes delete the back color for data were not matched in two sheets in previous time. may . you fix it please?
 
Upvote 0
Try this:

VBA Code:
Sub highlight_different_data()
  Dim sh1 As Worksheet, sh2 As Worksheet, sh3 As Worksheet, sh4 As Worksheet
  Dim dic As Object
  Dim i As Long, lr As Long
  
  Application.ScreenUpdating = False
  Set sh1 = Sheets("Main sheet")  'first
  Set sh2 = Sheets("Page-1")      'second
  Set sh3 = Sheets("Page-2")      'third
  Set sh4 = Sheets("RE")          'fourth
  Set dic = CreateObject("Scripting.Dictionary")
  dic.comparemode = vbTextCompare
  
  sh1.Rows("2:" & Rows.Count).Interior.Color = xlNone
  sh2.Rows("2:" & Rows.Count).Interior.Color = xlNone
  sh4.Rows("2:" & Rows.Count).ClearContents
  
  For i = 2 To sh3.Range("A" & Rows.Count).End(3).Row
    dic(sh3.Range("A" & i).Value) = Empty
  Next
  
  For i = 2 To sh1.Range("A" & Rows.Count).End(3).Row
    If Not dic.exists(sh1.Range("A" & i).Value) Then
      With sh1.Range("A" & i).Resize(1, 5)
        .Interior.Color = vbRed
        lr = sh4.Range("A" & Rows.Count).End(3)(2).Row
        sh4.Range("A" & lr).Resize(1, 5).Value = .Value
        sh4.Range("F" & lr).Value = "-"
      End With
    End If
  Next
  For i = 2 To sh2.Range("A" & Rows.Count).End(3).Row
    If Not dic.exists(sh2.Range("A" & i).Value) Then
      With sh2.Range("A" & i).Resize(1, 5)
        .Interior.Color = vbRed
        lr = sh4.Range("A" & Rows.Count).End(3)(2).Row
        sh4.Range("A" & lr).Resize(1, 5).Value = .Value
        sh4.Range("E" & lr).Value = "-"
        sh4.Range("F" & lr).Value = sh2.Range("E" & i).Value
      End With
    End If
  Next
End Sub
 
Upvote 0
that's very excellent ! I would ask from you about your code to understand if you don't mind . based on your code you match the data based on column A right or wrong? if it 's right if I would match based on column B,C,D together how can I mod . if you see it's simple just tell me if you see that create new code . forget it . just curoisity !
 
Upvote 0
based on your code you match the data based on column A right or wrong?
Right

if it 's right if I would match based on column B,C,D together how can I mod .

Then the data from columns B, C and D must be added to the index.
The same in validation. I show you the changes in blue:

Rich (BB code):
Sub highlight_different_data_2()
  Dim sh1 As Worksheet, sh2 As Worksheet, sh3 As Worksheet, sh4 As Worksheet
  Dim dic As Object, ky As Variant
  Dim i As Long, lr As Long
  
  Application.ScreenUpdating = False
  Set sh1 = Sheets("Main sheet")  'first
  Set sh2 = Sheets("Page-1")      'second
  Set sh3 = Sheets("Page-2")      'third
  Set sh4 = Sheets("RE")          'fourth
  Set dic = CreateObject("Scripting.Dictionary")
  dic.comparemode = vbTextCompare
  
  sh1.Rows("2:" & Rows.Count).Interior.Color = xlNone
  sh2.Rows("2:" & Rows.Count).Interior.Color = xlNone
  sh4.Rows("2:" & Rows.Count).ClearContents
  
  For i = 2 To sh3.Range("A" & Rows.Count).End(3).Row
    ky = sh3.Range("B" & i).Value & "|" & sh3.Range("C" & i).Value & "|" & sh3.Range("D" & i).Value
    dic(ky) = Empty
  Next
  
  For i = 2 To sh1.Range("A" & Rows.Count).End(3).Row
    ky = sh1.Range("B" & i).Value & "|" & sh1.Range("C" & i).Value & "|" & sh1.Range("D" & i).Value
    If Not dic.exists(ky) Then
      With sh1.Range("A" & i).Resize(1, 5)
        .Interior.Color = vbRed
        lr = sh4.Range("A" & Rows.Count).End(3)(2).Row
        sh4.Range("A" & lr).Resize(1, 5).Value = .Value
        sh4.Range("F" & lr).Value = "-"
      End With
    End If
  Next
  For i = 2 To sh2.Range("A" & Rows.Count).End(3).Row
    ky = sh2.Range("B" & i).Value & "|" & sh2.Range("C" & i).Value & "|" & sh2.Range("D" & i).Value
    If Not dic.exists(ky) Then
      With sh2.Range("A" & i).Resize(1, 5)
        .Interior.Color = vbRed
        lr = sh4.Range("A" & Rows.Count).End(3)(2).Row
        sh4.Range("A" & lr).Resize(1, 5).Value = .Value
        sh4.Range("E" & lr).Value = "-"
        sh4.Range("F" & lr).Value = sh2.Range("E" & i).Value
      End With
    End If
  Next
End Sub
 
Upvote 0
Solution
I'm glad to help you. Thanks for the feedback.
 
Upvote 0

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