Copy If Not Exists

billandrew

Well-known Member
Joined
Mar 9, 2014
Messages
743
Good Afternoon all

Looking to copy values [to another worksheet] from Column H which are not in Column T

This Script highlights column H values not in Column T - hoping for some insight how to attain the above request.

Thanks guys.

Code:
Sub CompareRangesII()    
    Dim r As Range
    Dim Dict As Object
    Dim Sht1 As Worksheet
    
    Set Dict = CreateObject("scripting.dictionary")
    Set Sht1 = Worksheets("Test")
    
    With Dict
    .CompareMode = vbTextCompare
    For Each r In Sht1.Range("T2", Sht1.Range("T" & Rows.Count).End(xlUp))
    If Not .exists(r.Value) Then .Add r.Value, r.Row
    Next r
    'Look through list in Column H and asertain if exists in Column T
    
    For Each r In Sht1.Range("H1", Sht1.Range("H" & Rows.Count).End(xlUp))
    If Not .exists(r.Value) Then
    r.Interior.Color = RGB(184, 205, 208)
    'this highlights Column H values Not in Column T list
    r.Font.Bold = True
        End If
        Next r
        End With
    Set r = Nothing


    End Sub
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Ok, try
Code:
Sub CompareRangesII()
   Dim r As Range, Rng As Range
   Dim Dict As Object
   Dim Sht1 As Worksheet
   
   Set Dict = CreateObject("scripting.dictionary")
   Set Sht1 = Worksheets("Test")
   
   With Dict
      .CompareMode = vbTextCompare
      For Each r In Sht1.Range("T2", Sht1.Range("T" & Rows.Count).End(xlUp))
         If Not .Exists(r.Value) Then .Add r.Value, r.Row
      Next r
      'Look through list in Column H and asertain if exists in Column T
      
      For Each r In Sht1.Range("H1", Sht1.Range("H" & Rows.Count).End(xlUp))
         If Not .Exists(r.Value) Then
            If Rng Is Nothing Then Set Rng = r.Offset(, -7).Resize(, 12) Else Set Rng = Union(Rng, r.Offset(, -7).Resize(, 12))
            r.Interior.Color = RGB(184, 205, 208)
            'this highlights Column H values Not in Column T list
            r.Font.Bold = True
         End If
      Next r
   End With
   Set r = Nothing
   If Not Rng Is Nothing Then Rng.Copy Sheets("Sheet4").Range("A1")
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,914
Messages
6,175,351
Members
452,638
Latest member
Oluwabukunmi

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