• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Compare two Excel Worksheets in different workbooks

benzoni86

New Member
Hi all,
i'm trying to put together a code to help me spot changes between two worksheets which are in two different workbooks. The layout of the worksheets is the same but data may change, be added, removed.
i have put together the following code by browsing different forums but this is not helping me: what i would like is to see old and new values as per code below, but also the name of the field for which value changed(row 8) and the article which changed(column D). This would help me sorting out if the change is relevant or not. also it would help to understand if it's a Change(different value for same cell),Addition(new value, previously blank) or deletion(blank, previously populated).can anyone support me?

Code:
Sub DoCompare()
  Dim WS As Worksheet
  Workbooks.Add
  For Each WS In Workbooks("1.xlsm").Worksheets
    Call CompareWorkbooks(WS, Workbooks("2.xlsm").Worksheets(WS.Name))
  Next
End Sub

Sub CompareWorkbooks(ByVal WS1 As Worksheet, _
                            ByVal WS2 As Worksheet)
Dim iRow As Integer
Dim iCol As Integer
Dim R1 As Range
Dim R2 As Range



  Worksheets.Add.Name = WS1.Name ' new book for the results
  Range("A1:E1").Value = Array("Address", "Difference", WS1.Parent.Name, WS2.Parent.Name, Field)
  Range("A2").Select
  For iRow = 1 To Application.Max(WS1.Range("A1").SpecialCells(xlLastCell).Row, _
                      WS2.Range("A1").SpecialCells(xlLastCell).Row)
    For iCol = 1 To Application.Max(WS1.Range("A1").SpecialCells(xlLastCell).Column, _
                      WS2.Range("A1").SpecialCells(xlLastCell).Column)
     
      Set R1 = WS1.Cells(iRow, iCol)
      Set R2 = WS2.Cells(iRow, iCol)
     
     
     
      ' compare the types to avoid getting VBA type mismatch errors.
      If TypeName(R1.Value) <> TypeName(R2.Value) Then
        NoteError R1.Address, "Type", R1.Value, R2.Value
      End If
    Next iCol
  Next iRow
  With ActiveSheet.UsedRange.Columns
    .AutoFit
    .HorizontalAlignment = xlLeft
  End With
End Sub

Sub NoteError(Address As String, What As String, V1, V2)
  ActiveCell.Resize(1, 4).Value = Array(Address, What, V1, V2)
  ActiveCell.Offset(1, 0).Select
  If ActiveCell.Row = Rows.Count Then
    MsgBox "Too many differences", vbExclamation
    End
  End If
End Sub
 

Attachments

  • 1.xlsm
    82.5 KB · Views: 5
  • 2.xlsm
    344.6 KB · Views: 3
benzoni86
Some questions ... notes ...
Is there any unique values that could know which rows should compare?
Those cannot compare between rows ... or ... can but eg if sorted ... added new row ... deleted any row ... result would be ... different.
Where is a time stamp to know ... which data is newer (if changed)?
Should 'only' data row should compare?
> What would be needed result output? Sample of that, please.
 
thanks for asking!
all rows should be compared and unique values would be in column D and starting from row 10. for what concerns newer and older data i think it depends on which is the first and the second file to compare. 1st sheet will be the oldest and second sheet selected would be the newest if this can work.
Here below is a sample of output that i would like to get. The macro above is limited since provides the address of the change, old and new value but does not reference to the article code(unique value) and to the field which got changed.

AddressDifference RiverSand StyleColour CodeName of field changedValue BeforeValue After
$H$11ADDEK00004308N RiverSand StyleColour CodeEK00004308N
$G$12ChangeEK00004308NSizeXSXS,S,M
$G$12ChangeEK00004308NMaterial Composition100% Cotton65% Polyester, 35% Cotton
 
benzoni86
Hmm? Did You read my questions?
Without unique values per row ... how to know? --- what to compare?
Your: what concerns newer and older data i think it depends on which is the first and the second file to compare.
> as I wrote: Those cannot compare between rows ... or ... can but eg if sorted ... added new row ... deleted any row ... result would be ... different.
Output:
Address - okay ... if I check $H$11 then those right side headers / values do not match which Your sample files ... hmm?
> Have You checked is Your idea of output ... useful?
There can be ... many changes!
Still something won't match ... ideas?
 
i probably misunderstood them but unique we would have a column with unique value and by default they would not be sorted so there would be a match between file 1 and file 2 and it would also be possible to define additions on top of changes.
the output idea does not match with the file content, is just an idea on how i think the output should be.
as i'm not super expert i don't know if there is a more efficient way to do this
 
benzoni86
What would prevent sorting? ... nothing?
Same case would come, after one row would delete (away) ... eg the 10th row from 1-file. ... then ALL data (rows) would be different!
Same affect, if someone ADD new row to top.
That's why, there should be something unique, that can know, which rows should compare.
Ideas?

> Output > Seems that there is not used 'comments'. Then ... I would use comments to show differences.
'Small red corners in those cells' and if move mouse over it - can see the result and compare it same time.
Also differences could highlite with cell colors.
 
GraH - Guido
Do that work if someone has
added new row 11 to one sheet or
deleted row 11 from one sheet or
sorted to different order one sheet?
Hi master @vletm, a sample of the result is something like this below. On the left there is the scope of analysis and the right part is the comparison result.
61403

Clearly, added or deleted rows will be detected. Different sorting will show up as "Entered Value Changes", but the feature will not detect the sorting difference.
This result can be exported in an Excel file as well.
 
@vletm
nothing would systemically prevent sorting or row addition/deletion,only internal process. Only alternative i can think to avoid this is to lookup the the article and if found go through the changes on the correct row. if not found, depending is on file 1 or two might result in addition or deletion. but apart from being able to describe it i would not be able to code it.
the output to me seems ok, it should only have the article, which is the core of the analysis, the name of the attribute changed(which is the header of the attribute) and old/new value...apart from this the rest(address and the type of change) are not really relevant.
 
@GraH - Guido
i tried to use it but is not supporting me since what i'm trying to retrieve are all the changes done on every article(information in column D) with old and new value but also including the header of the value that changed. Spreadsheet compare does a great job but is missing these pieces that for me would be mandatory to proceed with next steps of the analysis
 
GraH - Guido
Interesting feature ... quickly checked ... I didn't find how to see if there are differences between workbooks.
Sorting ... if whole table has sorted then ... values should keep same = no differences .. or how?
So far, looks something same as 'Share Workbook > Track Changes'-feature.
... but but, I would like to see those changes shown with Worksheet eg with comments.
 
Last edited:
benzoni86
Which column has Your used term 'the article'? ... or its code?
Could those be as like unique data?
Could You name some samples of Your needed differences? ( give exact cell reference )
 
Perhaps you can investigate if you can "extend" Compare Files via VBA. "Inquire" can be added on the ribbon via Customize Ribbon.
61404

I'm thinking extracting the compare result to a new workbook, and then add the missing pieces via some lookups.
 
HI @vletm
in the two files i shared the article is everything which is in column D, starting from row 10. they are unique data in the file.
all values change for a specific article for a specific attribute(attributes names are in row 8) should be listed as per example below:
dressDifferenceRiverSand StyleColour CodeName of field changedValue BeforeValue After
$D$12ADD
EK000075
RiverSand StyleColour Code
EK000075
$H$11Change
EK000074
M029 Colour Code
000

references should now be ok.
to make it lighter, if the difference is ADD(articles was not found in old file) there should be one row only mentioning the ADD. while if it's a change(article was there in old file) then only changes should be checked.
 
benzoni86
I would use something like in this file - not ready at all - but some ideas.
This way, You could SEE - where and what is/are different ... no need to have extra files/sheets.
(( I made this sample manually and only with small range! ))
Compare file 1.xlsm with file 2.xlsm ( 2nd file could select before start ... )
a) It would show with eg Yellow-cells those cells which has CHANGED --- there would be in comment - what is in other file
... also ... option ... if press button --- You could update that other files value to this file.
b) If this (1.xlsm) this misses some 'articles' (= D-column), then those values would show eg D12:G12-cells with own color and comments
... as well as those values could update to here.
c) there could be more above kind of options ...
'No matter' how many rows nor which orders - this would compare rows per those unique values.
Do other file need to show - which cells are different?
>>> Those colors and comments would disappear too, after needed steps.

> As I wrote, there is same kind of feature 'Shared File > Track Changes' ... but that needs different procedure with files.
 

Attachments

  • 1.xlsm
    81.8 KB · Views: 10
Back
Top