VBA Comparing two arrays in different spreadsheets

montecarlo2012

Well-known Member
Joined
Jan 26, 2011
Messages
984
Office Version
  1. 2010
Platform
  1. Windows
Hello. I have a little question about how to compare two arrays in different spreadsheets, AND highlight the matches. To make it short I just paste a small example.
ABCD
1 morrowrob60
2 krumholtzdavid20
3 hirschjudd10
4 ballardalimi5
5 lloydsabrina80
6 sumikaaya15
7 macnicolpeter19

sheet 1
ABCD
1 brunodylan100
2 farrdiane31
3 rawatnvai64
4 brownsophina967
5 sumikaaya15
6 macnicolpeter19

sheet 2
Thank you for reading this.
 
Last edited by a moderator:

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
montecarlo simulation is very famous for its whatif analysis.
run the following macro codes
Sub montecarlo()
Dim z As String, x As Integer, y As Integer, a As Integer, b As Integer
y = Cells(1, Columns.Count).End(xlToLeft).Column
x = Cells(Rows.Count, 1).End(xlUp).Row
For a = 2 To x
For b = 1 To y
If Sheets("sheet1").Cells(a, b) = Sheets("sheet2").Cells(a, b) Then
Sheets("sheet1").Cells(a, b).Font.ColorIndex = 3
Sheets("sheet1").Cells(a, b).Font.Bold = True
End If
Next b
Next a
MsgBox "complete"
End Sub
if 2 cells are identical, it will highlight it as bold and red font
Ravishankar
 
Upvote 0
Try this for data in sheets1 & sheet2.
Rich (BB code):
Sub MG27Oct00
Dim Rng1 As Range, Rng2 As Range, Dn As Range, n As Long
Dim Ac As Long, Ray As Variant, Q As Variant, K As Variant
With CreateObject("scripting.dictionary")
.CompareMode = vbTextCompare


Set Rng1 = Sheets("Sheet1").Cells(1).CurrentRegion
Set Rng2 = Sheets("Sheet2").Cells(1).CurrentRegion
Ray = Array(Rng1, Rng2)


For Ac = 0 To 1
    For Each Dn In Ray(Ac)
        If Not .Exists(Dn.Value) Then
            ReDim nRay(1 To 2)
            Set nRay(Ac + 1) = Dn
            .Add Dn.Value, nRay
        Else
            Q = .Item(Dn.Value)
             Set Q(Ac + 1) = Dn
           .Item(Dn.Value) = Q
        End If
    Next Dn
Next Ac

For Each K In .keys
    If .Item(K)(1) <> "" And .Item(K)(2) <> "" Then
        .Item(K)(1).Font.Color = vbRed
        .Item(K)(2).Font.Color = vbRed
    End If
Next K
End With
End Sub
Regards Mick
 
Last edited by a moderator:
Upvote 0
Thank you MickG. I like it. worked perfect
 
Last edited:
Upvote 0

Forum statistics

Threads
1,221,501
Messages
6,160,177
Members
451,629
Latest member
MNexcelguy19

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