+ Reply to Thread
Results 1 to 9 of 9

Extract unique values from range

Hybrid View

esbencito Extract unique values from... 09-26-2018, 05:36 AM
Sintek Re: Extract unique values... 09-26-2018, 07:19 AM
esbencito Re: Extract unique values... 09-26-2018, 11:05 PM
czl103 Re: Extract unique values... 09-26-2018, 11:18 PM
AlKey Re: Extract unique values... 09-26-2018, 11:53 PM
EXCELBENCH Re: Extract unique values... 09-30-2018, 06:38 AM
protonLeah Re: Extract unique values... 09-27-2018, 12:03 AM
Sintek Re: Extract unique values... 09-27-2018, 12:32 AM
esbencito Re: Extract unique values... 09-27-2018, 01:38 AM
  1. #1
    Forum Contributor
    Join Date
    07-13-2017
    Location
    Hong Kong
    MS-Off Ver
    MS Office 365
    Posts
    481

    Extract unique values from range

    Hi all,

    I have a range with a maximum of 3 distinct values. How can I extract these 1-3 values with VBA and sort them at the same time?

    e.g.

    Column A

    SS17
    SS17
    SS17
    SS18
    SS18
    SS18
    SS19
    SS19

    Then I want my code to store these 3 values in ascending order and assign them to a variable.

    Dim L1 As String
    Dim L2 As String
    Dim L3 As String
    
    L1 = "SS17"
    L2 = "SS18"
    L3 = "SS19"

  2. #2
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,957

    Re: Extract unique values from range

    Not sure if this is what you mean...
    Option Explicit
    
    Sub esbencito()
    Dim Val, i As Long, L(1 To 3) As String
    Application.ScreenUpdating = False
    With Sheet1.UsedRange.Rows
        Range(.Cells(1, 1), .Cells(.Count, 1)).AdvancedFilter xlFilterCopy, , .Range("C1"), True
        Range(.Cells(2, 3), .Cells(.Count, 3)).Sort Key1:=Range("C2"), Order1:=xlAscending, Header:=xlNo
        With .Range("C1").CurrentRegion:  Val = .Value:  .Clear:  End With
        For i = 2 To UBound(Val)
            L(i - 1) = Val(i, 1)
            Debug.Print L(i - 1)
        Next i
    End With
    Application.ScreenUpdating = True
    End Sub
    Last edited by Sintek; 09-26-2018 at 07:26 AM.
    Good Luck...
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the [★ Add Reputation] to left of post window...
    Also....Add a comment if you like!!!!
    And remember...Mark Thread as Solved...
    Excel Forum Rocks!!!

  3. #3
    Forum Contributor
    Join Date
    07-13-2017
    Location
    Hong Kong
    MS-Off Ver
    MS Office 365
    Posts
    481

    Re: Extract unique values from range

    hmm... this doesn't seem to be feasible in my data set as I am already using a filter. Also, I need to assign the 3 values to 3 different variables as I am using it for something else within my code.
    Attached Files Attached Files
    Last edited by esbencito; 09-26-2018 at 11:09 PM.

  4. #4
    Registered User
    Join Date
    03-11-2009
    Location
    BeiJing,China
    MS-Off Ver
    Excel 2016
    Posts
    11

    Re: Extract unique values from range


  5. #5
    Forum Guru AlKey's Avatar
    Join Date
    07-20-2009
    Location
    Lakeland, FL USA
    MS-Off Ver
    Microsoft Office 2010/ Office 365
    Posts
    8,903

    Re: Extract unique values from range

    Try to use formula
    Enter in B2 and copy down
    Formula: copy to clipboard
    =IFERROR(INDEX(A$2:A$9,MATCH(0,INDEX(COUNTIF(B$1:B1,A$2:A$9),,),)),"")

    v A B
    1
    2 SS17 SS17
    3 SS17 SS18
    4 SS17 SS19
    5 SS18
    6 SS18
    7 SS18
    8 SS19
    9 SS19
    If you like my answer please click on * Add Reputation
    Don't forget to mark threads as "Solved" if your problem has been resolved

    "Nothing is so firmly believed as what we least know."
    --Michel de Montaigne

  6. #6
    Forum Contributor
    Join Date
    03-23-2015
    Location
    Manila, Philippines
    MS-Off Ver
    2016
    Posts
    201

    Re: Extract unique values from range

    Quote Originally Posted by AlKey View Post
    Try to use formula
    Enter in B2 and copy down
    Formula: copy to clipboard
    =IFERROR(INDEX(A$2:A$9,MATCH(0,INDEX(COUNTIF(B$1:B1,A$2:A$9),,),)),"")

    v A B
    1
    2 SS17 SS17
    3 SS17 SS18
    4 SS17 SS19
    5 SS18
    6 SS18
    7 SS18
    8 SS19
    9 SS19

    *deleted.. apologies
    Last edited by EXCELBENCH; 09-30-2018 at 12:24 PM.

  7. #7
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,926

    Re: Extract unique values from range

    Option Explicit
    Sub Macro1()
        Dim LR As Long
        Dim Codes As Variant
        LR = Sheets("recap").Cells(Rows.Count, "C").End(xlUp).Row
        Range("AB2").EntireColumn.ClearContents
    
        Range("C1").Resize(RowSize:=LR).AdvancedFilter _
            Action:=xlFilterCopy, _
            CopyToRange:=Range("AB2"), _
            Unique:=True
            
        Range("AB2").Select
        Selection.Delete _
            Shift:=xlUp
            
        Codes = WorksheetFunction.Transpose(Range("AB2:AB4").Value)
        Range("AB2").EntireColumn.ClearContents
    End Sub
    Ben Van Johnson

  8. #8
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,957

    Re: Extract unique values from range

    hmm... this doesn't seem to be feasible in my data set
    Obviously you would have had to make changes to accommodate your data set
    Option Explicit
    
    Sub esbencito()
    Dim Val, i As Long, L(1 To 3) As String
    Application.ScreenUpdating = False
    With Sheets("Recap").UsedRange.Rows
        Range(.Cells(1, 3), .Cells(.Count, 3)).AdvancedFilter xlFilterCopy, , .Range("AB1"), True
        Range(.Cells(2, 28), .Cells(.Count, 28)).Sort Key1:=Range("AB2"), Order1:=xlAscending, Header:=xlNo
        With .Range("AB1").CurrentRegion:  Val = .Value:  .Clear:  End With
        For i = 2 To UBound(Val)
            L(i - 1) = Val(i, 1)
            Debug.Print L(i - 1)
        Next i
    End With
    Application.ScreenUpdating = True
    End Sub

  9. #9
    Forum Contributor
    Join Date
    07-13-2017
    Location
    Hong Kong
    MS-Off Ver
    MS Office 365
    Posts
    481

    Re: Extract unique values from range

    Thanks all! I ended up with a mix of both solutions and it seems to work just fine!

    Sub GetSeasonCodes()
        
        Dim LR As Long
        Dim SS_Codes As Variant
        
        Application.ScreenUpdating = False
        Application.Calculation = xlCalculationManual
        
        LR = Sheets("Recap").Cells(Rows.Count, Range("Attribute_SuperSeason").Column).End(xlUp).Row
        Sheets("Control").Range("A3").EntireColumn.ClearContents
    
        Sheets("Recap").Range("" & ColName(Range("Attribute_SuperSeason")) & "1").Resize(RowSize:=LR).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Sheets("Control").Range("A3"), Unique:=True
            
        Sheets("Control").Range("A3").Delete Shift:=xlUp
        
        Sheets("Control").Range("A3:A10").Sort Key1:=Sheets("Control").Range("A3"), Order1:=xlAscending, Header:=xlNo
            
        SS_Codes = WorksheetFunction.Transpose(Sheets("Control").Range("A3:A5").Value)
        
        Sheets("Control").Range("A3").EntireColumn.ClearContents
        
        Sheets("Recap").Range(Sheets("Recap").Cells(1, 1), Sheets("Recap").Cells(1, Columns.Count)).AutoFilter
        
        MsgBox SS_Codes(1) & "   " & SS_Codes(2) & "   " & SS_Codes(3)
        
        Application.Calculation = xlCalculationAutomatic
        Application.ScreenUpdating = True
    
    End Sub
    Didn't know that the advanced filter method is common to use in VBA to find unique values. It's probably even a lot faster than some of the other array calculations...
    Last edited by esbencito; 09-27-2018 at 01:40 AM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Replies: 11
    Last Post: 10-27-2016, 07:33 PM
  2. [SOLVED] Extract Unique values in range
    By YasserKhalil in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 11-08-2014, 06:39 AM
  3. Replies: 2
    Last Post: 02-13-2013, 03:14 PM
  4. Replies: 3
    Last Post: 11-24-2011, 06:11 AM
  5. Replies: 3
    Last Post: 09-20-2011, 05:05 PM
  6. Extract Unique Values, Then Extract Again to Remove Suffixes
    By Karl Burrows in forum Excel General
    Replies: 23
    Last Post: 06-25-2005, 08:05 PM

Tags for this Thread

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