Compare every cell of every worksheet in two workbooks

jscranton

Well-known Member
Joined
May 30, 2011
Messages
707
I am trying to track down changes between 120 pairs of workbooks (that should be identical but someone changed some data).

I have a list of filePaths for both workbooks. Plan is to open both, loop through each worksheet in both, load the used range of each worksheet into two arrays, compare each, and then flag errors.

Two issues:

(1) I am stuck on the variables for loading the used range into two arrays.

(2) For the second workbook "rWB", what's the easiest way to tell if a worksheet that exist in oWB doesn't exist in the potentially changed worksheet? I suppose I could just create a UDF to test it but wanted to see if there was an easier way using an error handler.

Code:
For i = 2 To LR

    oPath = ws.Range("B" & i).Value
    rPath = ws.Range("D" & i).Value
    
    Set oWB = Workbooks.Open(Filename:=oPath)
    Set rWB = Workbooks.Open(Filename:=rPath)
    hasErrors = False
    
   For Each w In oWB.Sheets
   
'Here is where I am stuck


        Set r = w.UsedRange


        varSheetA = ?
        varSheetB = ?


        For iRow = LBound(varSheetA, 1) To UBound(varSheetA, 1)
            For iCol = LBound(varSheetA, 2) To UBound(varSheetA, 2)
                If varSheetA(iRow, iCol) = varSheetB(iRow, iCol) Then
                    ' Cells are identical.
                    ' Do nothing.
                Else
		     hasErrors = True
		End if
            Next iCol
        Next iRow
    Next w
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
I figured this out. Now to figure out how to tell if a whole worksheet is missing.

Thanks,

J

Code:
   For Each w In oWB.Sheets   
        Set r = w.UsedRange
        rAdd = r.Address


        varSheetA = oWB.Sheets(w.Name).Range(rAdd)
        varSheetB = rWB.Sheets(w.Name).Range(rAdd)
 
Upvote 0
You can use a function like
Code:
Public Function ShtExists(ShtName As String, Optional wBk As Workbook) As Boolean
    If wBk Is Nothing Then Set wBk = ActiveWorkbook
    On Error Resume Next
    ShtExists = (LCase(wBk.Sheets(ShtName).Name) = LCase(ShtName))
    On Error GoTo 0
End Function
Code:
For Each w In oWB.Sheets
      
   If SheetExists(w.Name, rwb) Then
           Set r = w.UsedRange
           '
           '
           '
           '
   End If
Next w
 
Upvote 0

Forum statistics

Threads
1,214,606
Messages
6,120,479
Members
448,967
Latest member
visheshkotha

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