VBA Compare several sheets with eachother

DavyJones90

Board Regular
Joined
Feb 23, 2022
Messages
62
Office Version
  1. 365
Platform
  1. Windows
HI guys, trying to create a macro which compares the values in 3 workbooks to eachother and then puts the outcome on a fourth workbook in the exact same cell as in the 3 checked, a little background knowledge is that there are 3 Analysts for a project. The analysis of these 3 are supposed to be compared on a daily basis, now sometimes it will happen that there are only 2 at which point these two should be compared and differences noted (this can be done based on the date (meaning if only 2 input todays date the third is ignored.

This is what I got. It ain't working and I have no Idea how to extend and it to checking 3 workbooks.


VBA Code:
Option Explicit

Sub Comparebooks()
 
  Dim Analyst1 As Workbook, Analyst2 As Workbook, AutoArrows As Workbook
  Dim ws1 As Worksheet, ws2 As Worksheet, wsMatch As Boolean
  Dim cell As Range
  'Open a file dialog to select workbooks (optional)
 

  Set Analyst1 = Workbooks("C:\Users\david_5oni1yh\OneDrive\Desktop\Server\Analyst1\Analyst1.xlsm\") ' This Ain't working (SCRIPT OUT OF RANGE)
  Set Analyst2 = Workbooks("C:\Users\david_5oni1yh\OneDrive\Desktop\Server\Analyst2\Analyst2.xlsm\")  ' This Ain't working (SCRIPT OUT OF RANGE)
 
  If Analyst1.Worksheets.Count = Analyst2.Worksheets.Count Then
 
    'Loop through worksheets
    For Each ws1 In Analyst1.Worksheets
      wsMatch = False
      For Each ws2 In Analyst2.Worksheets
        If ws1.Name = ws2.Name Then
          wsMatch = True
          'Compare the 2 worksheets
          For Each cell In ws1.Range("A1").CurrentRegion
            If cell.Value <> ws2.Range(cell.Address).Value Then
                ThisWorkbook.cell.Interior.Color = vbYellow
                MsgBox "Mismatch cell " & cell.Address & " in worksheet " & ws1.Name
            End If
          Next cell
          Exit For
        End If
      Next ws2
      If wsMatch = False Then ThisWorkbook.Tab.Color = vbYellow
    Next ws1
  Else
    MsgBox "Worksheets mismatch"
  End If
 
  End Sub
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Helpers out there probably want to know more.

How do you want it to compare? How results would be like? Compare A and B, A and C, and then B and C to get 3 difference result?
Do List A, B and C are of the same length like there sure be a match in each list? If not, then the length in result when comparing between A to B and B to A would be different.

Perhaps you could illustrate with dummy list on how the result should look like. This will help a lot I believe for those helpers out there.
 
Upvote 0

Forum statistics

Threads
1,215,004
Messages
6,122,656
Members
449,091
Latest member
peppernaut

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