Compare Multiple Columns with Multiple Columns

Kent Heng

New Member
Joined
Nov 16, 2016
Messages
3
Hi all,

I have 3 base columns of data to compare against 3 other columns of data, and i want to highlight the differences between them. Problem is, the 3 columns to check against, might not be in sequence with the 3 base columns.

Example as below:

Column 1Column 2Column 3Column 4Column 5Column 6
POLPODContainerNoPOLPODContainerNo
PGLAEZACPTTRHU3305091PGMTKAEJEAXAAB5961250
PGLAEMYPGUDRYU2142162PGLAEBDCGPTRHU3305091
PGLAEIDSUBBSIU2922939PGLAEAEJEADRYU2142162
PGLAEAEJEAXAAB5961250PGLAEIDSUBBSIU2922939

<tbody>
</tbody>

The first 3 columns (in black), are the base columns and are correct. The next 3 columns (in red), could have errors in any of the 3 columns. I want to be able to spot the errors. The ContainerNo in Column 3 will be fixed and unique, while POL & POD can be duplicates.

The ideal outcome i want is as below (errors to be highlighted - blue in this example), while correct to remain unchanged:

Column 1Column 2Column 3Column 4Column 5Column 6
POLPODContainerNoPOLPODContainerNo
PGLAEZACPTTRHU3305091PGMTKAEJEAXAAB5961250
PGLAEMYPGUDRYU2142162PGLAEBDCGPTRHU3305091
PGLAEIDSUBBSIU2922939PGLAEAEJEADRYU2142162
PGLAEAEJEAXAAB5961250PGLAEIDSUBBSIU2922939

<tbody>
</tbody>

My thinking is that the formula will be based on Column 3 (ContainerNo), checking against Column 6 (ContainerNo), if match, then Check Column 1 against Column 4, and Column 2 against Column 5. If Column 6 (ContainerNo) does not match with Column 3 (ContainerNo), to highlight that particular ContainerNo in Column 6. (AKA ContainerNo is unique).

Would need the solution (and explanation) in VBA, as the macro will be stored in a worksheet to be used by a department.

Hope the above is clear enough.

Appreciate any help!!

Cheers :)
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
How about
Code:
Sub Checkdata()
   Dim Cl As Range
   
   With CreateObject("scripting.dictionary")
      .CompareMode = vbTextCompare
      For Each Cl In Range("C2", Range("C" & Rows.Count).End(xlUp))
         If Not .exists(Cl.Value) Then .Add Cl.Value, Array(Cl.Offset(, -2).Value, Cl.Offset(, -1).Value)
      Next Cl
      For Each Cl In Range("F2", Range("F" & Rows.Count).End(xlUp))
         If Not .exists(Cl.Value) Then
            Cl.Interior.Color = 45678
         ElseIf .Item(Cl.Value)(0) <> Cl.Offset(, -2).Value Then
            Cl.Offset(, -2).Interior.Color = 45678
         ElseIf .Item(Cl.Value)(1) <> Cl.Offset(, -1).Value Then
            Cl.Offset(, -1).Interior.Color = 45678
         End If
      Next Cl
   End With
End Sub
 
Upvote 0
Would another possibility be to use Conditional Formatting (applied by vba)?

Code:
Sub Apply_CF()
  Dim lr As Long
  
  lr = Range("A" & Rows.Count).End(xlUp).Row
  With Range("D2:F" & lr)
    .FormatConditions.Delete
    .Resize(, 2).FormatConditions.Add Type:=xlExpression, Formula1:=Replace("=AND(COUNTIF($C$2:$C$#,$F2)>0,COUNTIFS($C$2:$C$#,$F2,A$2:A$#,D2)=0)", "#", lr)
    .Resize(, 2).FormatConditions(1).Font.Color = vbBlue
    .Columns(3).FormatConditions.Add Type:=xlExpression, Formula1:=Replace("=ISNA(MATCH(F2,$C$2:$C$#,0))", "#", lr)
    .Columns(3).FormatConditions(1).Font.Color = vbBlue
  End With
End Sub
 
Upvote 0
Hi Peter,

Your code produces the same result as Fluff's (above).

Thanks for your help!!

Would another possibility be to use Conditional Formatting (applied by vba)?

Code:
Sub Apply_CF()
  Dim lr As Long
  
  lr = Range("A" & Rows.Count).End(xlUp).Row
  With Range("D2:F" & lr)
    .FormatConditions.Delete
    .Resize(, 2).FormatConditions.Add Type:=xlExpression, Formula1:=Replace("=AND(COUNTIF($C$2:$C$#,$F2)>0,COUNTIFS($C$2:$C$#,$F2,A$2:A$#,D2)=0)", "#", lr)
    .Resize(, 2).FormatConditions(1).Font.Color = vbBlue
    .Columns(3).FormatConditions.Add Type:=xlExpression, Formula1:=Replace("=ISNA(MATCH(F2,$C$2:$C$#,0))", "#", lr)
    .Columns(3).FormatConditions(1).Font.Color = vbBlue
  End With
End Sub
 
Upvote 0
Hi Peter,

Your code produces the same result as Fluff's (above).
There is a difference, though it may not be relevant to your circumstances.
With my code, if any values in the sheet are subsequently changed, the colours would, if relevant, change automatically without having to re-run the code.
 
Upvote 0

Forum statistics

Threads
1,215,734
Messages
6,126,545
Members
449,317
Latest member
chingiloum

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