+ Reply to Thread
Results 1 to 3 of 3

Compare two ranges, extract exceptions

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    11-02-2007
    Location
    Bryne, Norway
    MS-Off Ver
    Microsoft Office Excel 2010
    Posts
    230

    Compare two ranges, extract exceptions

    I want to compare two ranges (Rng1 and Rng2) and end up with a third range with values in Rng1 that doesn't exist in Rng2. There are several ways to do this, but I want to end up with a third range with no empty lines.

    I have attached an example. I can use a VBA macro or function. Both are OK.
    Attached Files Attached Files
    Last edited by mkvassh; 06-07-2010 at 03:31 AM.

  2. #2
    Forum Expert MickG's Avatar
    Join Date
    11-23-2007
    Location
    Banbury,Oxfordshire
    Posts
    2,650

    Re: Compare two ranges, extract exceptions

    Hi, Try this:-
    Sub MG04Jun34
    Dim Rng As Range, Rng2 As Range, Dn As Range, n As Long
    Set Rng = Range(Range("A2"), Range("A" & Rows.Count).End(xlUp))
    Set Rng2 = Range(Range("B2"), Range("B" & Rows.Count).End(xlUp))
    Set Rng = Union(Rng, Rng2)
    ReDim ray(1 To Rng.Count)
        With CreateObject("scripting.dictionary")
            .CompareMode = vbTextCompare
            For Each Dn In Rng
                If Not .Exists(Dn.Value) Then
                    n = n + 1
                    .Add Dn.Value, n
                    If Dn.Column = 1 Then ray(n) = Dn.Value
                Else
                    If Dn.Column = 2 Then
                          ray(.Item(Dn.Value)) = ""
                    End If
                End If
            Next
    End With
    With Range("C2").Resize(n)
        .Value = Application.Transpose(ray)
        .SpecialCells(xlCellTypeBlanks).Delete
     End With
    End Sub
    Regards Mick

  3. #3
    Forum Contributor
    Join Date
    11-02-2007
    Location
    Bryne, Norway
    MS-Off Ver
    Microsoft Office Excel 2010
    Posts
    230

    Re: Compare two ranges, extract exceptions

    Thank you :-)

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1