How to Compare Two Excel Workbooks and Highlight Differences in Excel VBA

In this tutorial, I will explain how to compare two Excel Workbooks and Highlight Differences in Excel VBA macros.

Now suppose that we have two files as described below:-

File 01- “Compare Two Sheets_Old”

Compare Two Excel Workbooks and Highlight Differences in Excel VBA
Compare Two Excel Workbooks and Highlight Differences in Excel VBA_Image_01

and File-02 “Compare Two Sheets_New”

Compare Two Excel Workbooks and Highlight Differences in Excel VBA
Compare Two Excel Workbooks and Highlight Differences in Excel VBA_Image_02

Both workbooks have different worksheets such as “Jan-22” and “Feb-22”.

And we do not know if there are any differences between these two files with all the sheets.

Here in this example, we have two worksheets in both files but this VBA macro will work if you have multiple worksheets also.

And now we will write our VBA macro to find the differences between these two worksheets.

First of all, go to the Excel Ribbon and click on the Developer tab in Excel.

If you are not finding the ‘Developer’ tab in your Excel then use the below steps to get it.

Go to the File menu in Excel.

Click on ‘Option’ and you will get the below Excel Options dialogue box.

Click on “Customize Ribbon” and then go to the second box, as you can see in the image below.

See that there is a ‘Developer’ option with a check box, mark the tick on this check box, and click on the ‘Ok’ button at the bottom.

Compare Two Excel Workbooks And Highlight Differences in Excel VBA
Compare Two Excel Workbooks And Highlight Differences in Excel VBA

Once you press the “Ok’ button below, your ‘Developer’ tab will appear in the Excel Ribbon.

Now use the instruction below.

Compare Two Excel Workbooks and Highlight Differences

Go to the ‘Developer’ tab and select ‘Visual Basic’ from the listed options.

Click on ‘Visual Basic’ option and you will get the Visual Basic Application window.

In this window, go to the ‘Insert’ tab and click on ‘Module’ from the listed options.

Compare Two Excel Workbooks and Highlight Differences in Excel VBA
Compare Two Excel Workbooks and Highlight Differences in Excel VBA

See below the ‘Module’, where we will write our VBA code.

Compare Two Excel Workbooks and Highlight Differences in Excel VBA
Compare Two Excel Workbooks and Highlight Differences in Excel VBA_Image

Use the below VBA code in this section.

Compare Two Excel Workbooks and Highlight Differences in Excel VBA
Compare Two Excel Workbooks and Highlight Differences in Excel VBA_Image

If you want to copy this code, you can copy it from the below box.

Sub Compare_Two_Workbooks()
 
 ‘Declare the variable for both the workbooks
 
  Dim wb1 As Workbook
  Dim wb2 As Workbook
  Dim ws1 As Worksheet
  Dim ws2 As Worksheet
 
 ‘Make sure both the files must be open
   
  ‘Set each of the 2 workbooks to object variables
  Set wb1 = Workbooks(“Compare Two Sheets_Old.xlsm”)
  Set wb2 = Workbooks(“Compare Two Sheets_New.xlsm”)
 
  If wb1.Worksheets.Count = wb2.Worksheets.Count Then
 
    ‘Loop through worksheets
    For Each ws1 In wb1.Worksheets
     
      For Each ws2 In wb2.Worksheets
       
        If ws1.Name = ws2.Name Then
       
          ‘Compare the 2 worksheets
          For Each cell In ws1.Range(“A1”).CurrentRegion
           
            If cell.Value <> ws2.Range(cell.Address).Value Then
               
                cell.Interior.Color = vbYellow
              
            End If
          Next cell
        End If
      Next ws2
    Next ws1
   End If
 End Sub
Compare Two Excel Workbooks and Highlight Differences_Image

Just have a look at this VBA code, and then execute this VBA code by pressing the ‘F5’ key from the keyboard shortcut to run the VBA macro.

Compare Two Excel Workbooks and Highlight Differences
Compare Two Excel Workbooks and Highlight Differences_Image

Once you will press ‘F5’ or run the macro by the ‘Run’ button, and see that you will find all the differences between the workbooks of files with all the worksheets with the differences.

Compare Two Excel Workbooks and Highlight Differences
Compare Two Excel Workbooks and Highlight Differences_Image

See the differences between the ‘Jan-22’ sheets for both files.

See the differences between the ‘Feb-22’ sheets for both files.

Compare Two Excel Workbooks and Highlight Differences
Compare Two Excel Workbooks and Highlight Differences_Image

Here in this tutorial, we have only two files but if you have multiple files then this macro will work exactly as it did in these two sheets.

Things to remember

All the sheet numbers must be equal in both files, i.e. the sheet number files contain the same sheet number that file 2 needs to contain also.

If you want to learn more about this, please visit Microsoft Office Support.

Please feel free to put your comments or suggestions in the comment box.

Thanks

Related Post

Calculate the Difference between Two Dates Dynamically

How to Extract Data Between Two Date Ranges in Excel VBA

Combine Data from Multiple Worksheets into a Single in Excel VBA

Excel VBA Macro: – Apply Filter and Copy, Paste Data Into Another Worksheet

3 Easy Methods to Transpose Data in Excel (Step-by-Step Guide)

How to Copy Data From a Closed Workbook in Excel VBA

9 Different Ways to Create New Sheets in Excel VBA

How to Convert Excel Files to CSV Format File

How to Filter the Data as You Type in Excel VBA

VBA Macro to Dynamically Copy and Paste from One Sheet to Another

Leave a Comment

Your email address will not be published. Required fields are marked *