+ Reply to Thread
Results 1 to 9 of 9

Extract unique values and their count from a two dimensional Array

  1. #1
    Registered User
    Join Date
    05-17-2012
    Location
    kerala
    MS-Off Ver
    Excel 2007
    Posts
    10

    Extract unique values and their count from a two dimensional Array

    I want a button that can be deployed on any Excel Worksheet. It does the following:
    It checks column A..B, AA... AZ... until it finds a column that has only empty cells.
    In each column, the macro program will find each filled-cell and add them to the end of a particular column (say the column to the right of the button).
    After this operation, there will be one column with a lot of cells (that have text data).
    The the macro program produce one simple report, showing:
    1. How many unique value there are
    2. How many records there are for each unique value.
    Those two report can be to the right of the 'data column'.

    Example:
    Col A Col B Col C
    gold wood porcelain
    silver gold silver
    gold fire
    bronze melon
    apple

    then create a button. If you click on that button and it give you

    gold 3
    silver 2
    bronze 1
    apple 1
    wood 1
    fire 1
    melon 1
    porcelain 1

    Then the job is done.

  2. #2
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Extract unique values and their count from a two dimensional Array

    Are you just trying to create a report with all the unique identifiers and their count? The Scripting Dictionary is a very fast way to do this. Please upload a workbook with all the different version that could appear and we can help you with this.
    Please consider:

    Thanking those who helped you. Click the star icon in the lower left part of the contributor's post and add Reputation.
    Cleaning up when you're done. Mark your thread [SOLVED] if you received your answer.

  3. #3
    Registered User
    Join Date
    05-17-2012
    Location
    kerala
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: Extract unique values and their count from a two dimensional Array

    I am sorry I cannot upload the workbook. It contain confidential info of my client. Yes, I am trying to create a report with all the unique identifiers to a new worksheet. The example is given in the post. the count of columns and rows can vary. So I need macro that checks from first cell (may be A2) to last cell (may be AG5000).

  4. #4
    Registered User
    Join Date
    05-17-2012
    Location
    kerala
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: Extract unique values and their count from a two dimensional Array

    I found this macro
    Sub CollateExtractUniqueIDs()

    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
    lColumns = lColumns + 1
    Application.ScreenUpdating = False
    For i = 1 To lColumns
    lLastRow = Cells(Rows.Count, i).End(xlUp).Row
    Range(Cells(1, i), Cells(lLastRow, i)).Copy _
    Cells(lNextRow, lColumns + 1)
    lNextRow = lNextRow + (lLastRow)
    Next 'i
    Application.ScreenUpdating = True
    Application.ScreenUpdating = False

    Cells(1, lColumns + 1).Value = "Combined"
    Range(Cells(1, lColumns + 1), Cells(lNextRow - 1, lColumns + 1)).AdvancedFilter _
    Action:=xlFilterCopy, _
    CopyToRange:=Cells(1, lColumns + 2), _
    Unique:=True
    Cells(1, lColumns + 2).Value = "Unique"
    Application.ScreenUpdating = True
    End Sub

    But, it will only list the unique values. I need the count of each value in next column
    Please help. This is very urgent.

  5. #5
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Extract unique values and their count from a two dimensional Array

    We don't need the actual data, you can use dummy data but the structure needs to remain the same.

  6. #6
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Extract unique values and their count from a two dimensional Array

    This may or may not fulfill your needs because you did not provide us with a dummy workbook. It works fine on a dummy sample I made but this may or may not be the same as yours.

    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    05-17-2012
    Location
    kerala
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: Extract unique values and their count from a two dimensional Array

    Thank you for posting that code Abousetta
    I made some changes to my own previous code

    Sub CollateExtractUniqueIDs()

    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
    lColumns = lColumns + 1
    Application.ScreenUpdating = False
    For i = 1 To lColumns
    lLastRow = Cells(Rows.Count, i).End(xlUp).Row
    Range(Cells(1, i), Cells(lLastRow, i)).Copy _
    Cells(lNextRow, lColumns + 1)
    lNextRow = lNextRow + (lLastRow)
    Next 'i
    Application.ScreenUpdating = True
    Application.ScreenUpdating = False

    Cells(1, lColumns + 1).Value = "Combined"
    Range(Cells(2, lColumns + 1), Cells(lNextRow - 1, lColumns + 1)).AdvancedFilter _
    Action:=xlFilterCopy, _
    CopyToRange:=Cells(1, lColumns + 2), _
    Unique:=True
    Cells(1, lColumns + 2).Value = "Unique"

    lLastRow = Cells(Rows.Count, lColumns + 2).End(xlUp).Row
    For i = 2 To lLastRow
    Cells(i, lColumns + 3).Value = Application.WorksheetFunction.CountIf(Range(Cells(2, lColumns + 1), Cells(lNextRow - 1, lColumns + 1)), Cells(i, lColumns + 2))
    Next i
    Application.ScreenUpdating = True
    End Sub

    But the counts are not sorted. I need to sort the values in last column (lColumns + 3) and second last column (lColumns + 2) based on the values in last (lColumns + 3) in descending order. Could you please help me with this.
    Thank you.

  8. #8
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Extract unique values and their count from a two dimensional Array

    THis will sort a two column Range. Change the range accoridng to your needs and whether or not there is a header or not, etc.

    Please Login or Register  to view this content.

  9. #9
    Registered User
    Join Date
    05-17-2012
    Location
    kerala
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: Extract unique values and their count from a two dimensional Array

    Thank you for all your help. I have changed your code a little bit to get my exact solution.
    I am pasting the code and a sample done.

    Code;


    Sub ExtractAndCountUnique()

    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
    lColumns = lColumns + 1
    Application.ScreenUpdating = False
    For i = 1 To lColumns
    lLastRow = Cells(Rows.Count, i).End(xlUp).Row
    Range(Cells(1, i), Cells(lLastRow, i)).Copy _
    Cells(lNextRow, lColumns + 1)
    lNextRow = lNextRow + (lLastRow)
    Next 'i
    Application.ScreenUpdating = True
    Application.ScreenUpdating = False

    Cells(1, lColumns + 1).Value = "Combined"
    Range(Cells(2, lColumns + 1), Cells(lNextRow - 1, lColumns + 1)).AdvancedFilter _
    Action:=xlFilterCopy, _
    CopyToRange:=Cells(1, lColumns + 2), _
    Unique:=True
    Cells(1, lColumns + 2).Value = "Unique"

    lLastRow = Cells(Rows.Count, lColumns + 2).End(xlUp).Row
    For i = 2 To lLastRow
    Cells(i, lColumns + 3).Value = Application.WorksheetFunction.CountIf(Range(Cells(2, lColumns + 1), Cells(lNextRow - 1, lColumns + 1)), Cells(i, lColumns + 2))
    Next i

    ActiveSheet.Range(Cells(1, lColumns + 2), Cells(lLastRow, lColumns + 3)).Select
    Selection.Sort Key1:=Range(Cells(1, lColumns + 3), Cells(lLastRow, lColumns + 3)), Order1:=xlDescending, Header:=xlYes
    Application.ScreenUpdating = True
    Cells(1, lColumns + 3).Value = "Count"
    End Sub


    Sample:
    a b c d e a
    f g h i j f
    k l m n o k
    p q r s t p
    u v w x y u
    z a s a s z
    a a a s s s

    Solution:

    Combined Unique Count
    a a 7
    f s 6
    k f 2
    p k 2
    u p 2
    z u 2
    a z 2
    b b 1
    g g 1
    l l 1
    q q 1
    v v 1
    a c 1
    a h 1
    c m 1
    h r 1
    m w 1
    r d 1
    w i 1
    s n 1
    a x 1
    d e 1
    i j 1
    n o 1
    s t 1
    x y 1
    a
    s
    e
    j
    o
    t
    y
    s
    s
    a
    f
    k
    p
    u
    z
    s

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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