VBA-Compare two lists to find differences and new records

Beginer1026

New Member
Joined
Sep 29, 2022
Messages
5
Office Version
  1. 365
Platform
  1. Windows
Hi,

How can I modify the code so that it can also compare columns C and H ("Nature") and highlight the difference (i.e. cell H4)...just like cell G10 and I15 ?

Compare and Find Differences.xlsm
ABCDEFGHIJ
1Document No. Amount NatureCompanyDocument No. Amount NatureCompany
2220537-W13,999.50ABCCompany A220537-W$13,999.50ABCCompany A
3220538-W3,840.00XYZCompany B220538-W$3,840.00XYZCompany B
4220539-W1,597.00ABCCompany B220539-W$1,597.00XYZCompany B
5220542-W810.69ZZZCompany B220542-W$810.69ZZZCompany B
6220543-W2,800.00ZZZCompany B220543-C$2,800.00ZZZCompany B
7220544-W604.06ZZZCompany C220544-W$604.06ZZZCompany C
8220547-W2,752.25XYZCompany M220545-W$40,046.40ZZZCompany C
9220611-C2,880.00ZZZCompany GGG220546-W$14,400.00XYZCompany P
10220612-W2,225.00ABCCompany EGG220547-W$2,752.28XYZCompany M
11220548-C$500.00XYZCompany BBC
12220549-C$1,920.00XYZCompany RET
13220609-C$4,960.00XYZCompany KL
14220610-C$3,594.88XYZCompany MBC
15220611-C$2,880.00ZZZCompany DGG
16220612-W$2,225.00ABCCompany EGG
17
Sheet1


VBA Code:
Sub CompareList()
    
    Application.ScreenUpdating = True
    Dim DocNo As Range
    Dim RngList As Object
    Dim rng As Range
    
    Set RngList = CreateObject("Scripting.Dictionary")
    Dim FoundDocNo As Range
    Dim LastRow2 As Long
    Dim lastRow As Long
    
    lastRow = Sheets("Sheet1").Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    For Each DocNo In Sheets("Sheet1").Range("F2:F" & lastRow)
        Set FoundDocNo = Sheets("Sheet1").Range("A:A").Find(DocNo, LookIn:=xlValues, lookat:=xlWhole)
        If Not FoundDocNo Is Nothing Then
            For Each rng In Sheets("Sheet1").Range("A" & FoundDocNo.Row & ":D" & FoundDocNo.Row)
                If Not RngList.Exists(rng.Value) Then
                    RngList.Add rng.Value, Nothing
                End If
            Next rng
            For Each rng In Sheets("Sheet1").Range("F" & DocNo.Row & ":I" & DocNo.Row)
                If Not RngList.Exists(rng.Value) Then
                    rng.Interior.ColorIndex = 6
                End If
            Next rng
        Else
            With Sheets("Sheet1").Range("F" & DocNo.Row & ":I" & DocNo.Row)
                .Interior.ColorIndex = 4
            End With
'           DocNo.Interior.ColorIndex = 4
        End If
    Next DocNo
    
    LastRow2 = Sheets("Sheet1").Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    For Each DocNo In Sheets("Sheet1").Range("a2:a" & lastRow)
        Set FoundDocNo = Sheets("Sheet1").Range("f:f").Find(DocNo, LookIn:=xlValues, lookat:=xlWhole)
        If FoundDocNo Is Nothing Then
            With Sheets("Sheet1").Range("A" & DocNo.Row & ":D" & DocNo.Row)
                .Interior.ColorIndex = 8
            End With
'            DocNo.Interior.ColorIndex = 8
        End If
    Next DocNo
End Sub

Thanks in advance !
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Welcome to the MrExcel Message Board!

You need to reset the dictionary object to remove the previously added values, RngList. You can add the following line in the first loop:

Rich (BB code):
        If Not FoundDocNo Is Nothing Then
            RngList.RemoveAll

Alternatively, I would use the following code without using multiple loops and a Dictionary object that would lead to false positive results in the way using it to find the matches in the existing code.

VBA Code:
Sub CompareListAlternative()
Dim sht As Worksheet
Dim rngA As Range
Dim rngF As Range
Dim rw As Range
Dim cll As Range
Dim fnd As Range

    Set sht = ActiveSheet
    Set rngA = sht.Range("A1").CurrentRegion
    Set rngA = rngA.Offset(1).Resize(rngA.Rows.Count - 1)
    
    Set rngF = sht.Range("F1").CurrentRegion
    Set rngF = rngF.Offset(1).Resize(rngF.Rows.Count - 1)
    
    rngA.Rows.Cells.Interior.Pattern = xlNone
    rngF.Rows.Cells.Interior.ColorIndex = 4
    
    For Each rw In rngA.Rows
        Set fnd = rngF.Columns(1).Find(rw.Cells(1).Value, LookIn:=xlValues, LookAt:=xlWhole)
        If fnd Is Nothing Then
            rw.Interior.ColorIndex = 8
        Else
            fnd.Resize(, rngF.Columns.Count).Interior.Pattern = xlNone
            For Each cll In rw.Cells
                If fnd.Cells(, cll.Column) <> cll.Value Then
                    fnd.Cells(, cll.Column).Interior.ColorIndex = 6
                End If
            Next cll
        End If
    Next rw

End Sub

Result:
Book1
ABCDEFGHI
1Document No. Amount NatureCompanyDocument No. Amount NatureCompany
2220537-W13999.50ABCCompany A220537-W13999.50ABCCompany A
3220538-W3840.00XYZCompany B220538-W3840.00XYZCompany B
4220539-W1597.00ABCCompany B220539-W1597.00XYZCompany B
5220542-W810.69ZZZCompany B220542-W810.69ZZZCompany B
6220543-W2800.00ZZZCompany B220543-C2800.00ZZZCompany B
7220544-W604.06ZZZCompany C220544-W604.06ZZZCompany C
8220547-W2752.25XYZCompany M220545-W40046.40ZZZCompany C
9220611-C2880.00ZZZCompany GGG220546-W14400.00XYZCompany P
10220612-W2225.00ABCCompany EGG220547-W2752.28XYZCompany M
11220548-C500.00XYZCompany BBC
12220549-C1920.00XYZCompany RET
13220609-C4960.00XYZCompany KL
14220610-C3594.88XYZCompany MBC
15220611-C2880.00ZZZCompany DGG
16220612-W2225.00ABCCompany EGG
Sheet1
 
Upvote 0
Solution
Welcome to the MrExcel Message Board!

You need to reset the dictionary object to remove the previously added values, RngList. You can add the following line in the first loop:

Rich (BB code):
        If Not FoundDocNo Is Nothing Then
            RngList.RemoveAll

Alternatively, I would use the following code without using multiple loops and a Dictionary object that would lead to false positive results in the way using it to find the matches in the existing code.

VBA Code:
Sub CompareListAlternative()
Dim sht As Worksheet
Dim rngA As Range
Dim rngF As Range
Dim rw As Range
Dim cll As Range
Dim fnd As Range

    Set sht = ActiveSheet
    Set rngA = sht.Range("A1").CurrentRegion
    Set rngA = rngA.Offset(1).Resize(rngA.Rows.Count - 1)
   
    Set rngF = sht.Range("F1").CurrentRegion
    Set rngF = rngF.Offset(1).Resize(rngF.Rows.Count - 1)
   
    rngA.Rows.Cells.Interior.Pattern = xlNone
    rngF.Rows.Cells.Interior.ColorIndex = 4
   
    For Each rw In rngA.Rows
        Set fnd = rngF.Columns(1).Find(rw.Cells(1).Value, LookIn:=xlValues, LookAt:=xlWhole)
        If fnd Is Nothing Then
            rw.Interior.ColorIndex = 8
        Else
            fnd.Resize(, rngF.Columns.Count).Interior.Pattern = xlNone
            For Each cll In rw.Cells
                If fnd.Cells(, cll.Column) <> cll.Value Then
                    fnd.Cells(, cll.Column).Interior.ColorIndex = 6
                End If
            Next cll
        End If
    Next rw

End Sub

Result:
Book1
ABCDEFGHI
1Document No. Amount NatureCompanyDocument No. Amount NatureCompany
2220537-W13999.50ABCCompany A220537-W13999.50ABCCompany A
3220538-W3840.00XYZCompany B220538-W3840.00XYZCompany B
4220539-W1597.00ABCCompany B220539-W1597.00XYZCompany B
5220542-W810.69ZZZCompany B220542-W810.69ZZZCompany B
6220543-W2800.00ZZZCompany B220543-C2800.00ZZZCompany B
7220544-W604.06ZZZCompany C220544-W604.06ZZZCompany C
8220547-W2752.25XYZCompany M220545-W40046.40ZZZCompany C
9220611-C2880.00ZZZCompany GGG220546-W14400.00XYZCompany P
10220612-W2225.00ABCCompany EGG220547-W2752.28XYZCompany M
11220548-C500.00XYZCompany BBC
12220549-C1920.00XYZCompany RET
13220609-C4960.00XYZCompany KL
14220610-C3594.88XYZCompany MBC
15220611-C2880.00ZZZCompany DGG
16220612-W2225.00ABCCompany EGG
Sheet1

Just tried adding RngList.RemoveAll to my code and it worked perfectly ! (y)
I will look into and try the code you suggested late, thanks a lot!
 
Upvote 0

Forum statistics

Threads
1,223,248
Messages
6,171,011
Members
452,374
Latest member
keccles

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