VBA combination columns lookup in another sheet.

jakobt

Active Member
Joined
May 31, 2010
Messages
337
Have a sheet1 with:
column A: with names
column B: with schools

In sheet2 I have:
column A: with names
column B: with schools

I want to look up the combination of A&B in each row in sheet 1 and colour them green if they are available in any row in sheet2.

I want to do it in vba as I also want to add a few other Or criterias later
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
How about
Code:
Sub ColourSome()
   Dim Cl As Range
   Dim Ws1 As Worksheet, Ws2 As Worksheet
   
   Set Ws1 = Sheets("Sheet1")
   Set Ws2 = Sheets("pcode")
   With CreateObject("scripting.dictionary")
      For Each Cl In Ws2.Range("A2", Ws2.Range("A" & Rows.Count).End(xlUp))
         .Item(Cl.Value & "|" & Cl.Offset(, 1).Value) = Empty
      Next Cl
      For Each Cl In Ws1.Range("A2", Ws1.Range("A" & Rows.Count).End(xlUp))
         If .exists(Cl.Value & "|" & Cl.Offset(, 1).Value) Then Cl.Resize(, 2).Interior.Color = vbGreen
      Next Cl
   End With
End Sub
 
Upvote 0
This is brilliant!!
Would like to add one more criteria.
First of all the upstanding criteria must be met.
Then if the value in column J in sheet1 for each row can be found in column E, F, G or H in sheet 2.
Only if this criteria is also met the color should be set to greeen.
 
Upvote 0
How about
Code:
Sub ColourSome()
   Dim cl As Range
   Dim Ws1 As Worksheet, Ws2 As Worksheet
   
   Set Ws1 = Sheets("Sheet1")
   Set Ws2 = Sheets("pcode")
   With CreateObject("scripting.dictionary")
      For Each cl In Ws2.Range("A2", Ws2.Range("A" & Rows.Count).End(xlUp))
         .Item(cl.Value & "|" & cl.Offset(, 1).Value) = Join(Application.Index(cl.Offset(, 4).Resize(, 4).Value, 1, 0), "|")
      Next cl
      For Each cl In Ws1.Range("A2", Ws1.Range("A" & Rows.Count).End(xlUp))
         If .exists(cl.Value & "|" & cl.Offset(, 1).Value) And cl.Offset(, 9).Value <> "" Then
            If InStr(1, .Item(cl.Value & "|" & cl.Offset(, 1).Value), cl.Offset(, 9).Value, vbTextCompare) > 0 Then
               cl.Resize(, 2).Interior.Color = vbGreen
            End If
         End If
      Next cl
   End With
End Sub
 
Upvote 0
Genious. I owe you one beer!

Final detail:
Instead of colouring 2 cells green. I would like the cell in column d, the cell in column j, and the cell in column N to be coloured green when the criterias are met.
 
Upvote 0
Before I spend yet more time rewriting the code, are you sure that this is the last time you'll "move the goal posts"?
 
Upvote 0
Ok, how about
Code:
Sub ColourSome()
   Dim cl As Range
   Dim Ws1 As Worksheet, Ws2 As Worksheet
   
   Set Ws1 = Sheets("Sheet1")
   Set Ws2 = Sheets("pcode")
   With CreateObject("scripting.dictionary")
      For Each cl In Ws2.Range("A2", Ws2.Range("A" & Rows.Count).End(xlUp))
         .Item(cl.Value & "|" & cl.Offset(, 1).Value) = Join(Application.Index(cl.Offset(, 4).Resize(, 4).Value, 1, 0), "|")
      Next cl
      For Each cl In Ws1.Range("A2", Ws1.Range("A" & Rows.Count).End(xlUp))
         If .Exists(cl.Value & "|" & cl.Offset(, 1).Value) And cl.Offset(, 9).Value <> "" Then
            If InStr(1, .Item(cl.Value & "|" & cl.Offset(, 1).Value), cl.Offset(, 9).Value, vbTextCompare) > 0 Then
               Intersect(cl.EntireRow, Ws1.Range("D:D,J:J,N:N")).Interior.Color = vbGreen
            Else
               Intersect(cl.EntireRow, Ws1.Range("D:D,J:J,N:N")).Interior.Color = vbRed
            End If
         [COLOR=#0000ff]Else
            Intersect(cl.EntireRow, Ws1.Range("D:D,J:J,N:N")).Interior.Color = vbRed[/COLOR]
         End If
      Next cl
   End With
End Sub
This will colour D,J & N either red or green.
If the value in cols A:B on sheet1 should not be red if they do not exist on sheet2 then remove the code in blue
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,204
Members
453,022
Latest member
RobertV1609

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