Macro To Compare Sheets and Highlight Common

gaspower

Board Regular
Joined
Jun 10, 2005
Messages
55
Hello,
I use the below to highlight similar between two sheets,
VBA Code:
Sub GreenDifference()
Dim LR As Long, i As Long, x As Variant
LR = Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
For i = 1 To LR
    x = Application.Match(Sheets("Sheet1").Range("A" & i).Value, Sheets("Sheet2").Columns("A"), 0)
    If IsNumeric(x) Then
        Sheets("Sheet1").Range("A" & i).Interior.Color = vbGreen
        Sheets("Sheet2").Range("A" & x).Interior.Color = vbGreen
    End If
Next i
End Sub

I would like to compare on more than two sheets, and the amount of sheets may vary. Can someone please help in allowing the code to expand over many sheets.

Thank you

EDIT
Also posted here Macro To Compare Sheets and Highlight Common
 
Last edited by a moderator:

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Do you want to highlight col A on sheet 1 if it matches col A in any other sheet?
 
Upvote 0
Ok, how about
VBA Code:
Sub gaspower()
   Dim Ary As Variant
   Dim i As Long
   Dim Ws As Worksheet, Ws1 As Worksheet
   Dim Cl As Range
   
   Set Ws1 = Sheets("Sheet1")
   With CreateObject("scripting.dictionary")
      .CompareMode = 1
      For Each Ws In Worksheets
         If Not Ws.Name = Ws1.Name Then
            Ary = Ws.Range("A1", Ws.Range("A" & Rows.Count).End(xlUp)).Value2
            If IsEmpty(Ary) Then Exit For
            For i = 1 To UBound(Ary)
               .Item(Ary(i, 1)) = Empty
            Next i
         End If
      Next Ws
      For Each Cl In Ws1.Range("A1", Ws1.Range("A" & Rows.Count).End(xlUp))
         If .Exists(Cl.Value) Then Cl.Interior.Color = vbGreen
      Next Cl
   End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,243
Members
449,075
Latest member
staticfluids

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