+ Reply to Thread
Results 1 to 4 of 4

Macro to extract unique values from a range and deposit them in another column

Hybrid View

  1. #1
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,049

    Re: Macro to extract unique values from a range and deposit them in another column

    I think this will do it.

    Note that it expects a heading in column G as per your sample workbook.

    Sub CollateAndExtractUniqueIDs()
    
    Dim lColumns As Long: lColumns = Cells(1, Columns.Count).End(xlToLeft).Offset(0, -1).Column
    Dim lNextRow As Long: lNextRow = 2
    Dim lLastRow As Long
    
    Application.ScreenUpdating = False
    For i = 1 To lColumns
        lLastRow = Cells(Rows.Count, i).End(xlUp).Row
        Range(Cells(2, i), Cells(lLastRow, i)).Copy _
            Cells(lNextRow, lColumns + 1)
        lNextRow = lNextRow + (lLastRow - 1)
    Next 'i
    
    Range(Cells(1, lColumns + 1), Cells(lNextRow - 1, lColumns + 1)).AdvancedFilter _
        Action:=xlFilterCopy, _
        CopyToRange:=Cells(1, lColumns + 2), _
        Unique:=True
    
    Application.ScreenUpdating = True
    Application.ScreenUpdating = False
    
    Cells(1, lColumns + 1).Value = "Combined"
    Cells(1, lColumns + 2).Value = "Unique"
    
    Application.ScreenUpdating = True
    End Sub

    Regards
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  2. #2
    Valued Forum Contributor Blake 7's Avatar
    Join Date
    10-01-2010
    Location
    Bogota, Colombia
    MS-Off Ver
    Excel 2010 64 bit and Excel 2007,
    Posts
    1,377

    Re: Macro to extract unique values from a range and deposit them in another column

    I praise you and may the good Lord bless you and your imediate family, and those in your street. And may Man U, your home town pummel Leeds tonight (sorry Domski - but i do have a few quid on it!)

    I've tried a few techniques to get this right and this solution was the best by a mile.

    Thanks
    Blake 7

    If your question has been answered, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

    If any member's response has helped to solve your problem, please consider using the scales icon on the top right of their post to show your appreciation.

    http://failblog.org/

+ 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