Comparing Two Ranges For Differences

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,564
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Is there an efficient way to code a comparison between two ranges (values) to determine whether there are any differences?

Range A
Excel Workbook
RSTUV
2CUE11Kelly0.2916670.625
3CUE2XNot Staffed0.2916670.625
4CUL12Jen0.6666671
5HPE1CTessa0.2916670.625
6HPE2XNot Staffed0.2916670.625
7HPL1AMorgan0.56250.895833
8HPL2XNot Staffed0.56250.895833
9RPE1CAshley B.0.2916670.625
10RPE2XNot Staffed0.2916670.625
11RPL1ACorey0.56250.895833
12RPL2XNot Staffed0.2916670.625
13WPE1CLauren0.2916670.625
14WPE2XNot Staffed0.2916670.625
15WPL1AKiana0.56250.895833
16WPL2XNot Staffed0.2916670.625
17PTE1XNot Staffed0.2916670.625
18PTE2XNot Staffed0.2916670.625
19PTL1AJessica0.6458330.8125
VAR_HOLD


Range B
Excel Workbook
RSTUV
25CUE11Kelly0.2916670.625
26CUE2XNot Staffed0.2916670.625
27CUL12Jen0.6666671
28HPE1CTessa0.2916670.625
29HPE2XNot Staffed0.2916670.625
30HPL1AMorgan0.56250.895833
31HPL2XNot Staffed0.56250.895833
32RPE1CAshley B.0.2916670.625
33RPE2XNot Staffed0.2916670.625
34RPL1ACorey0.56250.895833
35RPL2XNot Staffed0.2916670.625
36WPE1CLauren0.2916670.625
37WPE2XNot Staffed0.2916670.625
38WPL1AKiana0.56250.895833
39WPL2AJessica0.6458330.8125
40PTE1XNot Staffed0.2916670.625
41PTE2XNot Staffed0.2916670.625
42PTL1
VAR_HOLD


As a minimum, I need to a simple TRUE or FALSE as to whether there are differences between the two. In a perfect world, a list of changes would be cool, but I fear that may take some intense coding. Maybe meeting somewhere in the middle with a total number of differences would be acceptable.

Thank you in advance to anyone that wishes to help me.
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Hi Ark68,

Do you need to know if each cell is different. I'm guessing the two should ideally be mirrors of one another but in the example row 39 doesn't match row 16. This could be highlighted with conditional formatting or are you needing code to output it to a sheet of listed differences?

Regards

Dave
 
Upvote 0
Hi Dave,
I just simply need to know if their are any differences in "range B" from "Range A". This worksheet isn't visible to he user, all the code requires is to know is if there are differences.

The plan ...

If Range B has ANY differences from Range A then ...
Save the worksheet before exiting.
Otherwise...
Exit without saving.


Now it would be a bonus knowing how many differences there are, and perhaps a list of what they are, but neither are necessary for the end goal.
 
Last edited:
Upvote 0
Try something along these lines

Code:
Sub mjm()
Dim rng1, rng2 As Range
Dim myCell As Range
Dim rEqual As Boolean
Dim msg As String


msg = ""
rEqual = True
Set rng1 = Sheet5.Range("R2:V19")
Set rng2 = Sheet5.Range("R25:V42")


For Each myCell In rng1
    If myCell.Value <> myCell.Offset(23, 0).Value Then
        rEqual = False
        msg = msg & myCell.Address & " - " & myCell.Value & " not equal to " & myCell.Offset(23, 0).Address & " - " & myCell.Offset(23, 0).Value & vbNewLine
    End If
Next myCell


If rEqual = False Then MsgBox msg


'If rEqual = False Then
'    ThisWorkbook.Sheets.Add
'    ActiveSheet.Range("A1").Value = msg
'    With ThisWorkbook
'        .Save
'        .Close
'    End With
'Else
'    ThisWorkbook.Close savechanges:=False
'End If
End Sub

I commented out the last bit that will create a blank sheet and copy the differences to it.

Hopefully this will set you on your way

HTH

Dave
 
Upvote 0
Very nice!! Thanks so much Dave!
Works like a charm and exactly what I was looking for.
I'm going to do a bit of research on 'Offset' and 'Address' ... two functions that I imagine can save a lot of time and effort.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,849
Messages
6,121,925
Members
449,056
Latest member
denissimo

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