I have a few sheets that I need to compare cells in one column in sheet1 vs one column in sheet2. If match is made (with an "alert" match also) , the code colors the cell. The code taken from this site is as follows:
As you can see, the goal is to run a comparison on column 1 sheet1 against column 1 in sheet2. It works perfectly but what I need to do is to run the same comparison against sheet 3, 4, 5 6, etc etc. I know there is a way to do this through an array but I can't figure it out. I can copy the code multiple times and just change the w2 reference to w2 = sheet3, w2 = sheet4 etc but that is inefficient. I am not clear how to loop the array for each sheet.
Thanks for the help in advance and this site has been hugely helpful in my daily work.
VBA Code:
Sub Compare()
Dim w1 As Worksheet
Dim w2 As Worksheet
Dim c As Range
Dim a As Range
Set w1 = Sheets("sheet1")
Set w2 = Sheets("sheet2")
With w1
For Each c In .Range("A1:A100")
Set a = w2.Columns(1).Find(c.Value, LookAt:=xlWhole)
If Not a Is Nothing Then
If w1.Cells(c.Row, 1).Value <> w2.Cells(a.Row, 1).Value And .Cells(c.Row, 2) = "ALERT" Then
w2.Cells(a.Row, 1).Interior.ColorIndex = 3
End If
End If
Next c
End With
End Sub
As you can see, the goal is to run a comparison on column 1 sheet1 against column 1 in sheet2. It works perfectly but what I need to do is to run the same comparison against sheet 3, 4, 5 6, etc etc. I know there is a way to do this through an array but I can't figure it out. I can copy the code multiple times and just change the w2 reference to w2 = sheet3, w2 = sheet4 etc but that is inefficient. I am not clear how to loop the array for each sheet.
Thanks for the help in advance and this site has been hugely helpful in my daily work.