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

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
how about
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 Else Set Rng = Union(Rng, r)
            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("Sheet1").Range("A1")
End Sub
 
Upvote 0
Fluff

Thank you for the quick response. Works great!

By adding an additional range Rng variable - Does this store the values I need?

not entirely sure what role the Union is playing..
 
Upvote 0
You're welcome

Union simply joins 2 or more ranges into 1 range, so that rather than copying the ranges one at a time, you can copy them all at the end.
 
Upvote 0
Thanks

I know you heard this before...:) How can I resize the range Column A through Column L and copy to....
 
Upvote 0
Try
Code:
Rng.Offset(, -7).Resize(, 12).Copy Sheets("Sheet1").Range("A1")
 
Upvote 0
I'm breaking at this point - unsure why:-?:-?:-?


Code:
'If Not Rng Is Nothing Then Rng.Offset(, -7).Resize(, 12).Copy Sheets("Sheet4").Range("A1")
 
Upvote 0
What's the actual message, 1004 could be a number of things.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

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