Compare two columns in two sheets and highlight values

fahadun

New Member
Joined
Jul 27, 2017
Messages
22
hello,
I am very new to vba programming. I am facing a new problem.
What i am trying to do here,
So i have two sheet, sheet1 and sheet2. Compare between sheet1 column "C" and sheet2 column "R".


  • First highlight green,those mc# in sheet1 column "C", that are common in sheet1 and also in sheet2
  • second highlight yellow, those mc# in sheet1 column "C", that mc# is in sheet1 but not in sheet2
  • third highlight red, those mc# in sheet2 column "R", that mc# is not in sheet1 but is in sheet2

Next compare between sheet1 column "B" and sheet2 column "O".


  • fourth highlight green,those wr# in sheet1 column "B", that are common in sheet1 and also in sheet2
  • fifth highlight yellow, those wr# in sheet1 column "B", that wr# is in sheet1 but not in sheet2
  • sixth highlight red, those wr# in sheet2 column "O", that wr# is not in sheet1 but is in sheet2
Thank you.
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
This is untested, but try the below. If you receive any errors, please let me know what the error is and what line it errors on.

Code:
Public Sub fahadun()
Dim d1          As Object, _
    d2          As Object
    
Dim ws1         As Worksheet, _
    ws2         As Worksheet
    
Dim LR1         As Long, _
    LR2         As Long
    
Dim i           As Long, _
    k           As Variant
    
Dim indexGreen  As Long, _
    indexYellow As Long, _
    indexRed    As Long
    
    
indexGreen = 4
indexYellow = 6
indexRed = 3
    
Set ws1 = Sheets("Sheet1")
Set ws2 = Sheets("Sheet2")

'Compare columns C and R
Set d1 = CreateObject("Scripting.Dictionary")
Set d2 = CreateObject("Scripting.dictionary")

LR1 = ws1.Range("C" & Rows.Count).End(xlUp).Row
LR2 = ws2.Range("R" & Rows.Count).End(xlUp).Row

For i = 2 To LR1
    If Not d1.Exists(ws1.Range("C" & i).Value) Then
        d1.Add ws1.Range("C" & i).Value, i
    End If
Next i

For i = 2 To LR2
    If Not d2.Exists(ws2.Range("R" & i).Value) Then
        d2.Add ws2.Range("R" & i).Value, i
    End If
Next i

For Each k In d1.Keys
    If d2.Exists(k) Then
        ws1.Range("C" & d1(k)).Interior.colorindex = indexGreen
    Else
        ws1.Range("C" & d1(k)).Interior.colorindex = indexYellow
    End If
Next k

For Each k In d2.Keys
    If Not d1.Exists(k) Then
        ws2.Range("R" & d2(k)).Interior.colorindex = indexRed
    End If
Next k

Set d1 = Nothing
Set d2 = Nothing

'Compare columns B and O
Set d1 = CreateObject("Scripting.Dictionary")
Set d2 = CreateObject("Scripting.dictionary")

LR1 = ws1.Range("B" & Rows.Count).End(xlUp).Row
LR2 = ws2.Range("O" & Rows.Count).End(xlUp).Row

For i = 2 To LR1
    If Not d1.Exists(ws1.Range("B" & i).Value) Then
        d1.Add ws1.Range("B" & i).Value, i
    End If
Next i

For i = 2 To LR2
    If Not d2.Exists(ws2.Range("O" & i).Value) Then
        d2.Add ws2.Range("O" & i).Value, i
    End If
Next i

For Each k In d1.Keys
    If d2.Exists(k) Then
        ws1.Range("B" & d1(k)).Interior.colorindex = indexGreen
    Else
        ws1.Range("B" & d1(k)).Interior.colorindex = indexYellow
    End If
Next k

For Each k In d2.Keys
    If Not d1.Exists(k) Then
        ws2.Range("O" & d2(k)).Interior.colorindex = indexRed
    End If
Next k

End Sub
 
Upvote 0
@MrKowz , Thank you very much for your help. It's working first three conditions. but getting messed up for last 3 conditions.
 
Upvote 0
most of the values in sheet1 column b is yellow some is regular white, and all the values in sheet2 column O is red.
 
Upvote 0
I just tested this with dummy data, and it worked as intended. Can you please provide some sample data that I can run it against?


Excel 2013/2016
BC
1
2b2
3c3
4ooidijf4
5d80
6djida6
7f7
88
99
1010
1111
1250
1360
Sheet1



Excel 2013/2016
OPQR
2c4
38id9w0q6
4a10
59i020
63
Sheet2
 
Upvote 0
Unfortunately, I cannot download files on this computer (at work). Can you provide a small list I can test against?

One thing I didn't ask... are the values unique in the columns? Meaning you won't see the same value twice in Sheet1 Column C? If there are duplicate values, how do you want these handled?
 
Upvote 0
in sheet1 Both column have repetitive values. if every duplicate value is highlighted, that will be good. in sheet2 every value is unique.


sheet1
ID (Work Request)

<colgroup><col width="64"></colgroup><tbody>
</tbody>
CPMS_CASE_ID (Work Request)

<colgroup><col width="64"></colgroup><tbody>
</tbody>
2571755

<colgroup><col width="64"></colgroup><tbody>
</tbody>
MC-136244

<colgroup><col width="64"></colgroup><tbody>
</tbody>
2571755

<colgroup><col width="64"></colgroup><tbody>
</tbody>
MC-136244

<colgroup><col width="64"></colgroup><tbody>
</tbody>
3923870

<colgroup><col width="64"></colgroup><tbody>
</tbody>
MC-138492

<colgroup><col width="64"></colgroup><tbody>
</tbody>
2587401

<colgroup><col width="64"></colgroup><tbody>
</tbody>
MC-152737

<colgroup><col width="64"></colgroup><tbody>
</tbody>
2587401

<colgroup><col width="64"></colgroup><tbody>
</tbody>
MC-185865

<colgroup><col width="64"></colgroup><tbody>
</tbody>
3290056

<colgroup><col width="64"></colgroup><tbody>
</tbody>
MC-185865

<colgroup><col width="64"></colgroup><tbody>
</tbody>
3860962

<colgroup><col width="64"></colgroup><tbody>
</tbody>

<colgroup><col width="64"></colgroup><tbody>
</tbody>
MC-208438

<colgroup><col width="64"></colgroup><tbody>
</tbody>

<tbody>
</tbody>
WR

<colgroup><col width="64"></colgroup><tbody>
</tbody>
CPMS

<colgroup><col width="64"></colgroup><tbody>
</tbody>
3799080

<colgroup><col width="64"></colgroup><tbody>
</tbody>
MC-258215

<colgroup><col width="64"></colgroup><tbody>
</tbody>
3860962

<colgroup><col width="64"></colgroup><tbody>
</tbody>
MC-271790

<colgroup><col width="64"></colgroup><tbody>
</tbody>
3690035

<colgroup><col width="64"></colgroup><tbody>
</tbody>
MC-185865

<colgroup><col width="64"></colgroup><tbody>
</tbody>
3894677

<colgroup><col width="64"></colgroup><tbody>
</tbody>
MC-247056

<colgroup><col width="64"></colgroup><tbody>
</tbody>
3988244

<colgroup><col width="64"></colgroup><tbody>
</tbody>
MC-229830

<colgroup><col width="64"></colgroup><tbody>
</tbody>
3976963

<colgroup><col width="64"></colgroup><tbody>
</tbody>
MC-193574

<colgroup><col width="64"></colgroup><tbody>
</tbody>
3974162

<colgroup><col width="64"></colgroup><tbody>
</tbody>
MC-279640

<colgroup><col width="64"></colgroup><tbody>
</tbody>

<tbody>
</tbody>
 
Upvote 0
i don't know why, but it's not letting me make tables. In sheet1 both columns have repetitive values, and it's in ascending order. if every repetitive value are highlighted that will be good. but in sheet2 every values are unique in both columns. i am gonna make a table when i go home.
 
Upvote 0
@MrKowz, still stuck at that problem, here is the model data
sheet1
BC
EG
WR#MC#AddComm
2571755

<tbody>
</tbody>
MC-136244

<tbody>
</tbody>
HIMROD ST-

<tbody>
</tbody>
2571755

<tbody>
</tbody>
MC-136244

<tbody>
</tbody>
HIMROD ST-

<tbody>
</tbody>
3517927

<tbody>
</tbody>
MC-209089

<tbody>
</tbody>
VAN DAM STok
3616397

<tbody>
</tbody>
MC-229268

<tbody>
</tbody>
QUEENS PLZ
3616397

<tbody>
</tbody>
MC-229268

<tbody>
</tbody>
QUEENS PLZ
3951681

<tbody>
</tbody>
218 ST
3894677 DITMARS ST
3894677

<tbody>
</tbody>
DITMARS ST

<tbody>
</tbody>

sheet2
O
R
WR#MC#
2571755

<tbody>
</tbody>
MC-136244

<tbody>
</tbody>
3616397

<tbody>
</tbody>
MC-209089

<tbody>
</tbody>
3860962

<tbody>
</tbody>
MC-185865

<tbody>
</tbody>
3894677

<tbody>
</tbody>

<tbody>
</tbody>
[/QUOTE]
 
Upvote 0

Forum statistics

Threads
1,213,536
Messages
6,114,207
Members
448,554
Latest member
Gleisner2

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