Matching based on 2 criterias

alvbnp

Board Regular
Joined
Jun 26, 2006
Messages
180
I have 2 worksheets for doing matching. The matchings goes like if column A = column A and column B = Column B in both worksheets, then match column E, and highlight those unmatched cell in column E in sheet1. Besides that, also highlight those items which appear in sheet1 but not found in sheet2.

Can this be done just using built in formula in Excel or I need a macro for it?

Thanks for helping.
 
sure;
Code:
Sub test()
Dim i As Long
For i = 1 To Sheets("Sheet1").Range("a" & Rows.Count).End(xlUp).Row
    With Sheets("Sheet2").Columns("a")
    Set C = .Find(Cells(i, "a").Value, LookIn:=xlValues)
    If Not C Is Nothing Then
    FF = C.Address
    Do
    If C.Offset(, 1) = Cells(i, "b") _
    And C.Offset(, 4) <> Cells(i, "e") Then
      Sheets("Sheet1").Cells(i, "e").Interior.ColorIndex = 6
      C.Offset(, 4).Interior.ColorIndex = 6
          End If
      Set C = .FindNext(C)
      Loop Until FF = C.Address
Else
Sheets("Sheet1").Cells(i, "e").Interior.ColorIndex = 4
    End If
    End With
Next
End Sub
 
Upvote 0

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).
It's doesn't seems to work correctly, I noticed it wrongly highlighted cells in column E in green colour.

While the code is actually using column A & B to do the matching, if possible, i think a better idea is to highlight those missing in column A & B. But for my case, highlight in column B for missing item is good enough if highlighting both is too much of work.
 
Upvote 0
how about?
else, you need to provide again a sample data;
Code:
Sub test()
Dim i As Long
For i = 1 To Sheets("Sheet1").Range("a" & Rows.Count).End(xlUp).Row
    With Sheets("Sheet2").Columns("a")
    Set c = .Find(Cells(i, "a").Value, LookIn:=xlValues)
    If Not c Is Nothing Then
    FF = c.Address
    Do
    If c.Offset(, 1) = Cells(i, "b") _
    And c.Offset(, 4) <> Cells(i, "e") Then
      Sheets("Sheet1").Cells(i, "e").Interior.ColorIndex = 6
      c.Offset(, 4).Interior.ColorIndex = 6
          End If
         Set c = .FindNext(c)
      Loop Until FF = c.Address
Else
Sheets("Sheet1").Cells(i, "a").Resize(, 2).Interior.ColorIndex = 4
    End If
    End With
Next
End Sub
 
Upvote 0
Still doesn't work. Maybe I rephrase a bit.

The current code working on searching column A and B in both sheets in order to match column E. Somehow, if either column A or B is not found in either one of the sheets, it won't highlight anything. This give false result.

Eg:
sheet1
1 | a | 100
2 | a | 100
2 | b | 100

sheet2
1 | a | 100
2 | b | 100

the second item (2 | a | 100) is missing in sheet2, should be highligted. The current coding simply skip it.
 
Upvote 0
ok. try this;
Code:
Sub test()
Dim i, ii As Long
For i = 1 To Sheets("Sheet1").Range("a" & Rows.Count).End(xlUp).Row
    With Sheets("Sheet2").Columns("a")
    Set c = .Find(Cells(i, "a").Value, LookIn:=xlValues)
    If Not c Is Nothing Then
    ff = c.Address
    Do
    If c.Offset(, 1) = Cells(i, "b") _
    And c.Offset(, 4) <> Cells(i, "e") Then
      Sheets("Sheet1").Cells(i, "e").Interior.ColorIndex = 6
      c.Offset(, 4).Interior.ColorIndex = 6
      End If
    Set c = .FindNext(c)
      Loop Until ff = c.Address
Else
Sheets("Sheet1").Cells(i, "a").Resize(, 2).Interior.ColorIndex = 4
    End If
    End With
Cells(i, "aa") = Cells(i, "a") & Cells(i, "b")
For ii = 1 To Sheets("Sheet2").Range("a" & Rows.Count).End(xlUp).Row
Sheets("Sheet2").Cells(ii, "aa") = Sheets("Sheet2").Cells(ii, "a") & Sheets("Sheet2").Cells(ii, "b")
Next
Next
For i = 1 To Sheets("Sheet1").Range("a" & Rows.Count).End(xlUp).Row
With Sheets("Sheet2").Columns("aa")
    Set fc = .Find(Cells(i, "aa").Value, , , xlWhole)
    If fc Is Nothing Then
Sheets("Sheet1").Cells(i, "a").Resize(, 2).Interior.ColorIndex = 4
    End If
End With
Next
Sheets("Sheet1").Columns("aa").ClearContents
Sheets("Sheet2").Columns("aa").ClearContents
End Sub
 
Upvote 0
agihcam, need some modification on the code.

The last part of the code highlighting missing items in sheet1, how to change it to highlight missing items in sheet2 as well?

Thanks.
 
Upvote 0
add another loop;
Code:
Sub test()
Dim i, ii As Long
For i = 1 To Sheets("Sheet1").Range("a" & Rows.Count).End(xlUp).Row
    With Sheets("Sheet2").Columns("a")
    Set c = .Find(Cells(i, "a").Value, LookIn:=xlValues)
    If Not c Is Nothing Then
    ff = c.Address
    Do
    If c.Offset(, 1) = Cells(i, "b") _
    And c.Offset(, 4) <> Cells(i, "e") Then
      Sheets("Sheet1").Cells(i, "e").Interior.ColorIndex = 6
      c.Offset(, 4).Interior.ColorIndex = 6
      End If
    Set c = .FindNext(c)
      Loop Until ff = c.Address
Else
Sheets("Sheet1").Cells(i, "a").Resize(, 2).Interior.ColorIndex = 4
    End If
    End With
Cells(i, "aa") = Cells(i, "a") & Cells(i, "b")
For ii = 1 To Sheets("Sheet2").Range("a" & Rows.Count).End(xlUp).Row
Sheets("Sheet2").Cells(ii, "aa") = Sheets("Sheet2").Cells(ii, "a") & Sheets("Sheet2").Cells(ii, "b")
Next
Next
For i = 1 To Sheets("Sheet1").Range("a" & Rows.Count).End(xlUp).Row
With Sheets("Sheet2").Columns("aa")
    Set fc = .Find(Cells(i, "aa").Value, , , xlWhole)
    If fc Is Nothing Then
Sheets("Sheet1").Cells(i, "a").Resize(, 2).Interior.ColorIndex = 4
    End If
End With
Next
'highlight sheet2
For i = 1 To Sheets("Sheet2").Range("a" & Rows.Count).End(xlUp).Row
With Sheets("Sheet1").Columns("aa")
    Set fc = .Find(Sheets("Sheet2").Cells(i, "aa").Value, , , xlWhole)
    If fc Is Nothing Then
Sheets("Sheet2").Cells(i, "a").Resize(, 2).Interior.ColorIndex = 4
    End If
End With
Next
Sheets("Sheet1").Columns("aa").ClearContents
Sheets("Sheet2").Columns("aa").ClearContents
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,032
Messages
6,122,772
Members
449,095
Latest member
m_smith_solihull

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