Reputation: 1
I am a newbie here, also a newbie in VBA.
I have two worksheets that I wanted to compare.
Let's say,
But the column position of DWG and SYM in sheet2 is not always the same so first I need to locate the positions of the column before comparing. Then highlight the comparison.
Take note also that the sheets have thousands of row data and multiple columns. But only two columns are needed to be compared.
This is the working code:
Sub LookForMatches()
Dim rng1 As Range, rng2 As Range, c1 As Range, c2 As Range
Dim rng3 As Range, rng4 As Range, c3 As Range, c4 As Range
'set ranges
Set rng1 = Sheets("datax").Range("C5", Sheets("datax").Range("C" & Rows.Count).End(xlUp))
Set rng2 = Sheets("datay").Range("AC4", Sheets("datay").Range("AC" & Rows.Count).End(xlUp))
Set rng3 = Sheets("datax").Range("F5", Sheets("datax").Range("F" & Rows.Count).End(xlUp))
Set rng4 = Sheets("datay").Range("AH4", Sheets("datay").Range("AH" & Rows.Count).End(xlUp))
'reset colour
rng1.Interior.Color = 16777215
rng2.Interior.Color = 16777215
rng3.Interior.Color = 16777215
rng4.Interior.Color = 16777215
'loop values in range
For Each c1 In rng1
If Not c1.Interior.ColorIndex = 16777215 And c1 <> "" And c1 <> 0 Then
For Each c2 In rng2
If c1 = c2 And c2.Address <> c1.Address Then
c1.Interior.Color = RGB(255, 255, 0)
c2.Interior.Color = RGB(255, 255, 0)
End If
Next c2
End If
Next c1
'loop values in next range
For Each c3 In rng3
If Not c3.Interior.ColorIndex = 16777215 And c3 <> "" And c3 <> 0 Then
For Each c4 In rng4
If c3 = c4 And c4.Address <> c3.Address Then
c3.Interior.Color = RGB(255, 255, 0)
c4.Interior.Color = RGB(255, 255, 0)
End If
Next c4
End If
Next c3
MsgBox ("Checking Done")
Application.Goto Sheets("datay").Range("AA1"), True
End Sub
But sheet2's column location is defined. However, it should not be defined based on the column number but on the header name because the position of column is varying.
Thank you so much.
Upvotes: 0
Views: 125
Reputation: 17493
I don't understand why you are doing this using VBA: I created two columns, A:A and B:B, and I used conditional formatting to colour cells, based on their presency in column A:A, based on this formula:
=IFNA(MATCH(B2;$A$2:$A$4;0);FALSE) // for colouring if found
=NOT(IFNA(MATCH(B2;$A$2:$A$4;0);FALSE)) // for colouring otherwise if not found
Upvotes: 0