Compare data between 2 workbooks

dvitale71

New Member
Joined
May 4, 2018
Messages
8
Hi,
I'm trying to compare and list add/deletes between column A in Current Workbook vs. Previous Workbook. I was able to locate some code and modified it and is working well but not exactly what I want.

  • The code below does compare both sheets and and produces a match vs. mismatch message box
  • I want to identify what's new (added) between Current Workbook vs. Previous Workbook
  • I want to identify what's old (removed) between Current Workbook vs. Previous Workbook
  • I want to insert above findings into a new tab on Current Workbook where Column A is adds and Column B is deletes

Code:
Sub WorkbookComparision()
    Dim i As Long
    Dim wb1ws1, wb2ws2
    Dim blnSame As Boolean
    
    wb1ws1 = Workbooks("Current.xlsx").Worksheets("MASTER").Range("A4:A2500").Value
    wb2ws2 = Workbooks("Previous.xlsx").Worksheets("MASTER").Range("A4:A2500").Value
    
    For i = LBound(wb1ws1) To UBound(wb1ws1)
        If wb1ws1(i, 1) = wb2ws2(i, 1) Then
            blnSame = True
        Else
            blnSame = False
            Exit For
        End If
    Next i
    
    If blnSame = True Then
        MsgBox "data is the same"
    Else
        MsgBox "data is different"
    End If
End Sub

Any help or guidance is appreciated.

Thank you.
 
Last edited by a moderator:

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Untested, but try
Code:
Sub WorkbookComparision()
   Dim wb1ws1, wb2ws2
   Dim Dic2 As Object
   Dim Wb1 As Workbook
   
   Set Dic2 = CreateObject("scripting.dictionary")
   Set Wb1 = Workbooks("Current.xlsx")
   wb1ws1 = Wb1.Worksheets("MASTER").Range("A4:A2500").Value
   wb2ws2 = Workbooks("Previous.xlsx").Worksheets("MASTER").Range("A4:A2500").Value
   
   With CreateObject("scripting.dictionary")
      For i = LBound(wb1ws1) To UBound(wb1ws1)
         If Not .exists(wb1ws1(i, 1)) Then .Add wb1ws1(i, 1), Nothing
      Next i
      For i = LBound(wb2ws2) To UBound(wb2ws2)
         If .exists(wb2ws2(i, 1)) Then
            .Remove wb2ws2(i, 1)
         ElseIf Not Dic2.exists(wb2ws2(i, 1)) Then
            Dic2.Add wb2ws2(i, 1), Nothing
         End If
      Next i
      Wb1.Sheets.Add(, Wb1.Sheets(Wb1.Sheets.Count)).Name = "Results"
      Wb1.Sheets("results").Range("A1").Resize(.Count).Value = Application.Transpose(.keys)
      Wb1.Sheets("results").Range("B1").Resize(Dic2.Count).Value = Application.Transpose(Dic2.keys)
   End With
End Sub
 
Upvote 0
This code worked great, however, are we able to add something if no changes occur between the two files. For example, if there are no changes can we make A1 state... "There are no changes reported this week". Currently, this scenario errors out.


Untested, but try
Code:
Sub WorkbookComparision()
   Dim wb1ws1, wb2ws2
   Dim Dic2 As Object
   Dim Wb1 As Workbook
   
   Set Dic2 = CreateObject("scripting.dictionary")
   Set Wb1 = Workbooks("Current.xlsx")
   wb1ws1 = Wb1.Worksheets("MASTER").Range("A4:A2500").Value
   wb2ws2 = Workbooks("Previous.xlsx").Worksheets("MASTER").Range("A4:A2500").Value
   
   With CreateObject("scripting.dictionary")
      For i = LBound(wb1ws1) To UBound(wb1ws1)
         If Not .exists(wb1ws1(i, 1)) Then .Add wb1ws1(i, 1), Nothing
      Next i
      For i = LBound(wb2ws2) To UBound(wb2ws2)
         If .exists(wb2ws2(i, 1)) Then
            .Remove wb2ws2(i, 1)
         ElseIf Not Dic2.exists(wb2ws2(i, 1)) Then
            Dic2.Add wb2ws2(i, 1), Nothing
         End If
      Next i
      Wb1.Sheets.Add(, Wb1.Sheets(Wb1.Sheets.Count)).Name = "Results"
      Wb1.Sheets("results").Range("A1").Resize(.Count).Value = Application.Transpose(.keys)
      Wb1.Sheets("results").Range("B1").Resize(Dic2.Count).Value = Application.Transpose(Dic2.keys)
   End With
End Sub
 
Upvote 0
How about
Code:
      Wb1.Sheets.Add(, Wb1.Sheets(Wb1.Sheets.Count)).Name = "Results"
      If .Count > 0 Then
         Wb1.Sheets("results").Range("A1").Resize(.Count).Value = Application.Transpose(.keys)
      Else
         Wb1.Sheets("results").Range("A1").Value = "There are no changes reported this week"
      End If
      If Dic2.Count > 0 Then
         Wb1.Sheets("results").Range("B1").Resize(Dic2.Count).Value = Application.Transpose(Dic2.keys)
      Else
          Wb1.Sheets("results").Range("B1").Value = "There are no changes reported this week"
      End If
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,157
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