+ Reply to Thread
Results 1 to 5 of 5

extract Unique Records from multipe columns

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    08-14-2006
    Location
    USA
    MS-Off Ver
    2019
    Posts
    686

    extract Unique Records from multipe columns

    I have data in 22 columns extending to various rows.

    Is there a way to extract the Unique Records (to a new location) from all 22 columns at once like you can with the Advanced Filter?

    (VBA is ok if there's no other way)

  2. #2
    Forum Contributor VBA Noob's Avatar
    Join Date
    04-25-2006
    Location
    London, England
    MS-Off Ver
    xl03 & xl 07(Jan 09)
    Posts
    11,988
    Here's a VBA way. Select area then run

    Sub ExtractDuplicateRecords()
    Dim c As Range
    Dim i As Long
    i = Selection.Columns.Count + 1
    Application.ScreenUpdating = False
    For Each c In Selection
    If Application.CountIf(Columns(i), c) = 0 Then
    Cells(65536, i).End(xlUp).Offset(1, 0) = c
    End If
    Next c
    With Cells(1, i)
    .Value = "Unique List"
    .EntireColumn.AutoFit
    End With
    Application.ScreenUpdating = True
    
    End Sub
    VBA Noob
    _________________________________________


    Credo Elvem ipsum etian vivere
    _________________________________________
    A message for cross posters

    Please remember to wrap code.

    Forum Rules

    Please add to your signature if you found this link helpful. Excel links !!!

  3. #3
    Forum Expert Ron Coderre's Avatar
    Join Date
    03-22-2005
    Location
    Boston, Massachusetts
    MS-Off Ver
    2013, 2016, O365
    Posts
    6,996

    extract Unique Records from multipe columns

    Here's an example demonstrating an approach you may be interested in:

    Assumptions:
    Sheet1 contains your data in cells A1:B10
    Sheet2 is where you want the extracted data to be displayed

    Using Sheet2:
    A1: EmpID
    B1: Age

    Insert>Name>Define
    Names in workbook: Sheet2!Extract
    Refers to: =Sheet2!$A$1:$B$1

    I1: EmpID
    I2: 24

    Insert>Name>Define
    Names in workbook: Sheet2!Criteria
    Refers to: =Sheet2!$I$1:$I$2

    Still using Sheet2:
    Insert>Name>Define
    Names in workbook: Sheet2!Database
    Refers to: =Sheet1!$A$1:$B$10

    (Notice: you are on Sheet2, and creating a Sheet2-level range name, but
    the referenced range is on Sheet1)

    The reason: An advanced filter cannot SEND data to another sheet, but
    it can PULL data from another sheet.

    Now...set up the Advanced Data Filter:
    <Data><Filter><Advanced Filter>
    Select: Copy to another location
    List Range: (press F3 and select Database)
    Criteria Range: (press F3 and select Criteria)
    Copy To: (press F3 and select Extract)
    Click [OK]

    Note: if you want to run that Advanced Data Filter repeatedly,
    you'll need to re-select Database each time
    ....OR...if you're feeling a bit ambitious...

    You can build a simple macro to automatically re-run the filter:
    Press [Alt]+[F11] to open the VBA editor
    Right click on the VBA Project folder for your workbook
    Select: Insert>Module

    Then, copy/paste this code into that module:
    PHP Code: 
    Option Explicit
    Sub PullMatchingData
    ()
    Range("Sheet2!Database").AdvancedFilter _
    Action
    :=xlFilterCopy_
    CriteriaRange
    :=Range("Sheet2!Criteria"), _
    CopyToRange
    :=Range("Sheet2!Extract"), _
    Unique
    :=False
    End Sub 
    To run the code:
    Tools>Macro>Macros (or [Alt]+[F8])
    Select and run: PullMatchingData

    To test, change the value of I2 and run it again.

    Is that something you can work with?
    Ron
    Former Microsoft MVP - Excel (2006 - 2015)
    Click here to see the Forum Rules

  4. #4
    Forum Contributor
    Join Date
    08-14-2006
    Location
    USA
    MS-Off Ver
    2019
    Posts
    686
    Thanks both,
    I used VBANoobs code

    Ron,
    Thanks for the explanation, I knew there was a way with a Named Range, but just naming the range containing the 22 columns didn't work for me! I'll try yours out later.

  5. #5
    Forum Contributor
    Join Date
    09-25-2004
    Posts
    269
    I'm assuming your columns are from C1 to X20


    Input formula in cell A2:

    =INDEX(C1:X20,1,1)

    Input formula in cell A3 and copy down.

    =INDEX($1:$65536,INT(MIN(IF(COUNTIF($A$2:A2,$C$1:$X$20)=0,1000*ROW($C$1:$X$20)+COLUMN($C$1:$X$20)))/1000),MOD(MIN(IF(COUNTIF(A$2:A2,$C$1:$X$20)=0,1000*ROW($C$1:$X$20)+COLUMN($C$1:$X$20))),1000))

    The formula is an-array need to hold down:

    Ctrl,Shift,Enter

    See the attachment below.

    Hope it helps!
    Attached Files Attached Files

+ 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