Compare two excel sheets And copy the differences

sofas

Well-known Member
Joined
Sep 11, 2022
Messages
559
Office Version
  1. 2021
  2. 2019
Platform
  1. Windows
I'm trying to compare several different columns
(Coloured in yellow) When there is any discrepancy in the mentioned columns, it is copied to the results sheet as shown in the image below. I've manually changed the background color for the differences just for clarity. I am ready to accept any proposed idea.


1.png

2.png

3.png





 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Please try

=CHOOSECOLS(FILTER(sheet1!A11:O18,(sheet1!A11:A18<>Sheet2!A11:A18)+(sheet1!B11:B18<>Sheet2!B11:B18+…...),0),1,2,5,8,14) for getting sheet 1 differences

1,2,5,8,14 at the last are the columns which are needed in the summary

=CHOOSECOLS(FILTER(sheet2!A11:O18,(sheet1!A11:A18<>Sheet2!A11:A18)+(sheet1!B11:B18<>Sheet2!B11:B18+…...),0),1,2,5,8,14) for getting sheet 1 differences

A short form of this, as columns A to N are involved is

=CHOOSECOLS(FILTER(Sheet1!A11:O18,MMULT(--(Sheet1!A11:O18<>Sheet2!A11:O18),SEQUENCE(COLUMNS(Sheet1!A10:O10),1,1,1))>0,0),1,2,5,8,14)---- for sheet 1 and
=CHOOSECOLS(FILTER(Sheet2!A11:O18,MMULT(--(Sheet1!A11:O18<>Sheet2!A11:O18),SEQUENCE(COLUMNS(Sheet2!A10:O10),1,1,1))>0,0),1,2,5,8,14)----for sheet 2

For getting the count of differences, in the summary sheet of differences, please do

=SUM(--(B4:F4<>H4:M4))
 
Upvote 0
Hello, thank you for your interest in my request. Although I was trying to get results with codes. I tried to put in the formulas, but unfortunately I am missing something that I don't know. Please put it in the attached file if you please. Thank you again
 
Upvote 0
I don't believe your version of Excel 2019 has the FILTER function.
You may want to look at Power Query. A google search will bring up links on how to compare columns.
Here is one.
 
Upvote 0
I don't believe your version of Excel 2019 has the FILTER function.
You may want to look at Power Query. A google search will bring up links on how to compare columns.
Here is one.
Thank you, I am now using the 2021 version
 
Upvote 0
Code:
Sub test()
    Dim a(1), b, i&, ii&, n&, x&, dic As Object
    Set dic = CreateObject("Scripting.Dictionary")
    With Sheets("sheet2")
        With .Range("a10", .Range("a" & Rows.Count).End(xlUp)).Resize(, 14)
            a(0) = Application.Index(.Value, Evaluate("row(2:" & .Rows.Count & ")"), [{1,2,5,8,14}])
        End With
    End With
    For i = 1 To UBound(a(0), 1)
        dic(a(0)(i, 1)) = Array(i, Join(Application.Index(a(0), i, 0), Chr(2)))
    Next
    With Sheets("sheet1")
        With .Range("a10", .Range("a" & Rows.Count).End(xlUp)).Resize(, 14)
            a(1) = Application.Index(.Value, Evaluate("row(2:" & .Rows.Count & ")"), [{1,2,5,8,14}])
        End With
    End With
    ReDim b(1 To UBound(a(1), 1), 1 To UBound(a(1), 2) * 2 + 2)
    For i = 1 To UBound(a(1), 1)
        If dic.exists(a(1)(i, 1)) Then
            If dic(a(1)(i, 1))(1) <> Join(Application.Index(a(1), i, 0), Chr(2)) Then
                n = n + 1
                For ii = 1 To UBound(a(1), 2)
                    b(n, ii) = a(1)(i, ii)
                    b(n, ii + UBound(b, 2) / 2) = a(0)(dic(a(1)(i, 1))(0), ii)
                    If b(n, ii) <> b(n, ii + UBound(b, 2) / 2) Then
                        b(n, UBound(b, 2)) = b(n, UBound(b, 2)) + 1
                    End If
                Next
            End If
        End If
    Next
    With Sheets("results")
        With .Rows("4:" & .Cells.SpecialCells(11).Row)
            .ClearContents: .Interior.ColorIndex = xlNone
        End With
        If n Then
            .[b4].Resize(n, UBound(b, 2)) = b
            With .Rows(3).SpecialCells(2).Areas(2)
                With .CurrentRegion.Resize(, .Columns.Count - 1)
                    .FormatConditions.Delete
                    .FormatConditions.Add 2, Formula1:="=" & .Cells(1).Address(0, 0) & "<>b3"
                    .FormatConditions(1).Interior.Color = rgbLightBlue
                End With
            End With
        End If
    End With
End Sub
 
Upvote 1
Solution
Code:
Sub test()
    Dim a(1), b, i&, ii&, n&, x&, dic As Object
    Set dic = CreateObject("Scripting.Dictionary")
    With Sheets("sheet2")
        With .Range("a10", .Range("a" & Rows.Count).End(xlUp)).Resize(, 14)
            a(0) = Application.Index(.Value, Evaluate("row(2:" & .Rows.Count & ")"), [{1,2,5,8,14}])
        End With
    End With
    For i = 1 To UBound(a(0), 1)
        dic(a(0)(i, 1)) = Array(i, Join(Application.Index(a(0), i, 0), Chr(2)))
    Next
    With Sheets("sheet1")
        With .Range("a10", .Range("a" & Rows.Count).End(xlUp)).Resize(, 14)
            a(1) = Application.Index(.Value, Evaluate("row(2:" & .Rows.Count & ")"), [{1,2,5,8,14}])
        End With
    End With
    ReDim b(1 To UBound(a(1), 1), 1 To UBound(a(1), 2) * 2 + 2)
    For i = 1 To UBound(a(1), 1)
        If dic.exists(a(1)(i, 1)) Then
            If dic(a(1)(i, 1))(1) <> Join(Application.Index(a(1), i, 0), Chr(2)) Then
                n = n + 1
                For ii = 1 To UBound(a(1), 2)
                    b(n, ii) = a(1)(i, ii)
                    b(n, ii + UBound(b, 2) / 2) = a(0)(dic(a(1)(i, 1))(0), ii)
                    If b(n, ii) <> b(n, ii + UBound(b, 2) / 2) Then
                        b(n, UBound(b, 2)) = b(n, UBound(b, 2)) + 1
                    End If
                Next
            End If
        End If
    Next
    With Sheets("results")
        With .Rows("4:" & .Cells.SpecialCells(11).Row)
            .ClearContents: .Interior.ColorIndex = xlNone
        End With
        If n Then
            .[b4].Resize(n, UBound(b, 2)) = b
            With .Rows(3).SpecialCells(2).Areas(2)
                With .CurrentRegion.Resize(, .Columns.Count - 1)
                    .FormatConditions.Delete
                    .FormatConditions.Add 2, Formula1:="=" & .Cells(1).Address(0, 0) & "<>b3"
                    .FormatConditions(1).Interior.Color = rgbLightBlue
                End With
            End With
        End If
    End With
End Sub

Great, he succeeded in that
 
Last edited:
Upvote 0
Tested in your workbook and it runs without problem here, so Bad luck.
 
Upvote 0
Yes, I tested your code on the results sheet after deleting row number 3, so the error message was shown, I think. Thank you very much. It works very well.
 
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,151
Members
453,021
Latest member
Justyna P

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