+ Reply to Thread
Results 1 to 44 of 44

Extracting unique values from a column

  1. #1
    Forum Contributor
    Join Date
    01-18-2013
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    641

    Extracting unique values from a column

    Hi All

    I'm using this formula to extract and list unique values from column F

    =IF(COUNTIF(F$3:F3,F3)=1,F3,"")

    It does the job , and lists the first occurrence of each item. Unfortunately it leaves blank cells where there is a repetition of the item.

    So instead of

    Susan
    John
    Harry
    Joanne
    Billy

    It gives

    Susan

    John
    Harry

    Joanne
    Billy

    Where Susan and Harry are repeated.

    Can someone think of a way to avoid the blank cells? Or some other (better!) way of making a unique list form the entries in column F? Maybe some VBA under the workbook tab? If it were able to be sorted alphabetically too that would be a real bonus.

    Grateful for any help.


    Very Best Wishes ,

  2. #2
    Forum Guru Pete_UK's Avatar
    Join Date
    12-31-2011
    Location
    Warrington, England
    MS-Off Ver
    Office 2019 (still learning)
    Posts
    24,724

    Re: Extracting unique values from a column

    Change the formula (assumed to be in G3) to this:

    =IF(COUNTIF(F$3:F3,F3)=1,MAX(G$2:G2)+1,"")

    then copy down. Then you can use this formula to get the unique names without spaces (e.g. in H3):

    =IFERROR(INDEX(F:F,MATCH(ROWS($1:1),G:G,0)),"")

    Copy this down until you start to get blanks.

    Hope this helps.

    Pete

  3. #3
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,410

    Re: Extracting unique values from a column

    Give this macro a try (it outputs to Column G starting on Row 3 - change red highlight if different output location is desired)...
    Please Login or Register  to view this content.

  4. #4
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,524

    Re: Extracting unique values from a column

    Quote Originally Posted by CDandVinyl View Post
    Can someone think of a way to avoid the blank cells? Or some other (better!) way of making a unique list form the entries in column F? Maybe some VBA
    If not header, give header in F2 and use AdvancedFilter with True in unique protocol, so no need of Formula or VBA.

    If VBA
    Please Login or Register  to view this content.
    Last edited by jindon; 10-22-2019 at 09:21 PM.

  5. #5
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,410

    Re: Extracting unique values from a column

    Quote Originally Posted by jindon View Post
    If not header, give header in F2 and use AdvancedFilter with True in unique protocol, so no need of Formula or VBA.

    If VBA
    Please Login or Register  to view this content.
    You might want to add some code to sort your output as the OP said "If it were able to be sorted alphabetically too that would be a real bonus."

  6. #6
    Forum Contributor
    Join Date
    01-18-2013
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    641

    Re: Extracting unique values from a column

    Hi All

    Thanks for your various solutions. I'm really grateful.

    I'm using column O starting in O3 , so I'm changing the assumed G3 to this in the code sent , but I'm getting errors unfortunately.

    The 'Sub UniqueAndSort()' routine gives 'Run Time' and 'Automation' errors in the line

    With CreateObject("System.Collections.ArrayList")

    I changed G3 to O3 in the code.

    I also tried the test2() routine and it works with error messages sometimes with 'Sort Method of range class failed' , and with unsorted output. Again , I changed the references to column G to column O in the original coding.

    I'll keep at them in the hope of getting them working. Perhaps you could advise. It would be good to get a kind of auto-updating on the date rather than manually running the macro but I don't think it's far off presently. I'm using excel 2003.


    Very Best Wishes ,
    Last edited by CDandVinyl; 10-22-2019 at 10:53 PM.

  7. #7
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,524

    Re: Extracting unique values from a column

    Use one of 3...
    If still error, need to see your workbook.

    To a worksheet code module.
    1) AdvancedFilter
    Please Login or Register  to view this content.
    2) Evaluate
    Please Login or Register  to view this content.
    3) ArrayList
    Please Login or Register  to view this content.
    Last edited by jindon; 10-22-2019 at 11:42 PM.

  8. #8
    Forum Contributor
    Join Date
    01-18-2013
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    641

    Re: Extracting unique values from a column

    Hi .

    Thanks for these.

    I assume each should be placed in the code section under the worksheet tab.

    I'm afraid none gave any reaction to the data. They didn't give errors either , but none generated the unique list in column O based on the data in column F.

    I'm grateful in any case for your help.


    Very Best Wishes ,

  9. #9
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,410

    Re: Extracting unique values from a column

    Quote Originally Posted by CDandVinyl View Post
    I'm afraid none gave any reaction to the data. They didn't give errors either
    What is in Column F... constants or formulas? If formulas, can you post the one in Cell F3 for us?

  10. #10
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,524

    Re: Extracting unique values from a column

    Then you will need to upload a sample workbook that doesn't work.

  11. #11
    Forum Contributor
    Join Date
    01-18-2013
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    641

    Re: Extracting unique values from a column

    Hi

    The values in column F are all constants. I'm trying to send a sorted unique list to O3 down.

    Thanks for your help.

    Very Best Wishes ,

  12. #12
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,410

    Re: Extracting unique values from a column

    Quote Originally Posted by CDandVinyl View Post
    The values in column F are all constants. I'm trying to send a sorted unique list to O3 down.
    Then you will need to heed jindon's suggestion and post a sample workbook for which the codes posted so far do not work so we can see what is different about your actual data compared to the samples we construct here to test our code on.

  13. #13
    Forum Contributor
    Join Date
    01-18-2013
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    641

    Re: Extracting unique values from a column

    HI

    OK I've attached a sample. Hopefully this will help.

    Thanks again.

    Very Best Wishes ,

  14. #14
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,410

    Re: Extracting unique values from a column

    I just tried the macro I posted in Message #3 just changing the G3 to O3 in the last line of code (before the last End With statement) and it ran fine producing a sorted list of 35 theater names. However, I just noticed you are using XL2003 which I no longer have available to me (I am using XL2010), so I cannot be sure if there are any incompatibilities with XL2003 and the call outs to the Dictionary and UserList objects. Maybe jindon has some knowledge about this. In any event, I am sure jindon will try his event codes (remember, mine was a macro) on your sample file and be back to you with his results.
    Last edited by Rick Rothstein; 10-23-2019 at 05:16 PM.

  15. #15
    Forum Contributor
    Join Date
    01-18-2013
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    641

    Re: Extracting unique values from a column

    OK thanks for getting back. Yes it may be down to version incompatibilities.

  16. #16
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,410

    Re: Extracting unique values from a column

    Something which may be blocking our codes from running fully or correctly... the worksheet you posted is password protected. I am not sure what protections you have engaged, but I was able to write out a list to the worksheet but the protection would not let me sort it. Try unprotecting the worksheet and then try running our codes again and see if they now work.

  17. #17
    Forum Contributor
    Join Date
    01-18-2013
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    641

    Re: Extracting unique values from a column

    I overlooked the protection when I sent the sample file , so apologies for that.

    I did try with an unprotected sheet , with no joy. I still get this 'automation error' message. I'll attach the unprotected sheet.

  18. #18
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,410

    Re: Extracting unique values from a column

    Here is an alternate macro that I have developed which I think (hope) will work for you. Output is to Column O starting at Row 3 (see last With statement if you need to change this)...
    Please Login or Register  to view this content.

  19. #19
    Forum Guru Pete_UK's Avatar
    Join Date
    12-31-2011
    Location
    Warrington, England
    MS-Off Ver
    Office 2019 (still learning)
    Posts
    24,724

    Re: Extracting unique values from a column

    Since posting my reply in Post #2 you have confirmed that you are using XL2003, and that you wanted the list of unique names to be sorted. You have also attached an unprotected file in Post #17.

    My original formulae will not work for you, as IFERROR was not available in XL2003, so with this latest file of yours I can show you how you can achieve your objective using formulae (if you are still interested in that), as you seem to be running into difficulties with the various VBA solutions that have been offered.

    I put this formula in cell N3 of your latest file:

    =IF(COUNTIF(F$3:F3,F3)=1,MAX(N$2:N2)+1,"")

    and copied down to the bottom of your data. This produces a simple sequence for unique records, each time a new name is encountered in column F. Those unique names can be extracted using this formula in O3:

    =IF(ROWS($1:1)>MAX(N:N),"",INDEX(F:F,MATCH(ROWS($1:1),N:N,0)))

    and this only needs to be copied down until you start to get blanks. Of course, this is not sorted, but another helper column can be used with this formula in cell P3:

    =COUNTIF($O$3:$O$37,"<="&O3)

    Copy this down to the bottom of your unique_names list, and it will give a series of numbers which represent the sort order for those names, and you can get the sorted list with this formula in Q3:

    =INDEX(O:O,MATCH(ROWS($1:1),P:P,0))

    Again, copy this down as required.

    This should work in XL2003, as the attached file demonstrates.

    Hope this helps.

    Pete
    Attached Files Attached Files

  20. #20
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,524

    Re: Extracting unique values from a column

    Change your current code to
    Please Login or Register  to view this content.

  21. #21
    Forum Contributor
    Join Date
    01-18-2013
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    641

    Re: Extracting unique values from a column

    Not sure my previous post got through. At any rate , thanks very much to all. I've got it working now. I think 2003 gives errors but I've by and large got around them.

    For example - Jindon this line

    [o3].Resize(Cells.SpecialCells(11).Row - 2).ClearContents

    gives an error saying that this command can't be used on an unprotected sheet. I unprotected and it runs fine. I don't know if it fails with absolutely any protection or whether the worksheet has to be completely unprotected. This would concern me a bit for the obvious reasons.


    Grateful for your time and expertise.
    Last edited by CDandVinyl; 10-23-2019 at 10:04 PM.

  22. #22
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,410

    Re: Extracting unique values from a column

    I am not sure it is what you want (it's a macro, not event code), but I am wondering if you saw the new code I posted in Message #18 yet or not?

  23. #23
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,524

    Re: Extracting unique values from a column

    Is the worksheet protected? what is the password?

  24. #24
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,524

    Re: Extracting unique values from a column

    I have to go out now, so if the worksheet is password protected, you need to unprotect first...
    Also tidied up.
    Please Login or Register  to view this content.
    Attached Files Attached Files

  25. #25
    Forum Contributor
    Join Date
    01-18-2013
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    641

    Re: Extracting unique values from a column

    Rick - yes I ran that and it worked fine! Thanks.

    Pete - That's running fine now too.

    Jindon - I ran the new code you sent and it runs perfectly , although curiously , the code changes the protected and unprotected elements form the original settings.

    When I protect the sheet , I allow users to

    Select locked cells
    Select unlocked cells
    Format cells
    Format columns
    Format rows
    Sort
    Use auto-filter
    Edit objects

    Once the event procedure has run , the protection is changed to allow only

    Select locked cells
    Select unlocked cells

    This seems strange , as I can see the code just switches the protection off then on again. It does nevertheless change the elements of the protection above. It does also mean that if I now save the sheet after the procedure has run , the original protection configuration is lost.


    I'm grateful for everyone's help. Different solutions to the issue , but all do the job.

    Last edited by CDandVinyl; 10-23-2019 at 10:59 PM.

  26. #26
    Forum Guru Pete_UK's Avatar
    Join Date
    12-31-2011
    Location
    Warrington, England
    MS-Off Ver
    Office 2019 (still learning)
    Posts
    24,724

    Re: Extracting unique values from a column

    If that takes care of your original question, please take a moment to select Thread Tools from the menu above and to the right of your first post in this thread, and mark the thread as SOLVED.

    Also, you might like to know that you can directly thank those who have helped you by clicking on the small "star" icon located in the lower left corner of any post that you have found to be helpful (not just in this thread - for any post that has helped you). This also adds to the reputation of the poster (the small green bars in the poster's profile).

    Pete

  27. #27
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,524

    Re: Extracting unique values from a column

    Quote Originally Posted by CDandVinyl View Post
    Jindon - I ran the new code you sent and it runs perfectly , although curiously , the code changes the protected and unprotected elements form the original settings.

    When I protect the sheet , I allow users to

    Select locked cells
    Select unlocked cells
    Format cells
    Format columns
    Format rows
    Sort
    Use auto-filter
    Edit objects

    Once the event procedure has run , the protection is changed to allow only

    Select locked cells
    Select unlocked cells
    You can add the protection protocols by changing
    Please Login or Register  to view this content.
    to
    Please Login or Register  to view this content.
    Last edited by jindon; 10-24-2019 at 06:44 AM.

  28. #28
    Forum Contributor
    Join Date
    04-12-2013
    Location
    Usually at work, in the UK
    MS-Off Ver
    Excel 2010
    Posts
    639

    Re: Extracting unique values from a column

    why dont you just copy the column of names onto another tab and use the remove duplicates option under data?

  29. #29
    Forum Guru Pete_UK's Avatar
    Join Date
    12-31-2011
    Location
    Warrington, England
    MS-Off Ver
    Office 2019 (still learning)
    Posts
    24,724

    Re: Extracting unique values from a column

    I don't think that Remove Duplicates was available in XL2003.

    Pete

  30. #30
    Forum Contributor
    Join Date
    01-18-2013
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    641

    Re: Extracting unique values from a column

    Ok thanks for all your help. All is running well now.

  31. #31
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,524

    Re: Extracting unique values from a column

    You are welcome and thanks for the rep.

  32. #32
    Forum Contributor
    Join Date
    01-18-2013
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    641

    Re: Extracting unique values from a column

    I have an additional question to this topic , relating to extracting unique values.

    In my workbook I have 5 sheets 2017 - 2021. In column F from F3 on each sheet I list constant values.

    I was wanting to open a 6th sheet ('Unique values') and list from B3 a unique list from column F from F3 down of the 5 worksheets , and then in C3 put how many times each value appears.

    These would be unique from column F from all the sheets. The sheets are password protected. The other sheets also have change event code.

    Can this be done with an event procedure under the tab on the new 'Unique values' sheet?

    Grateful for your advice.

  33. #33
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,524

    Re: Extracting unique values from a column

    Assuming
    1) Pass words for each sheets are common.
    2) All the sheets from 2017 - 2021 has same change events.
    3) you want it to happen when any change in any sheet made.

    Delete all the individual Change event from 2017 - 2021 and paste the code onto ThisWorkbook code module.
    Please Login or Register  to view this content.
    Last edited by jindon; 10-24-2019 at 11:49 PM.

  34. #34
    Forum Contributor
    Join Date
    01-18-2013
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    641

    Re: Extracting unique values from a column

    Hi Jindon

    Many thanks for this. I applied the code with no effect , I'm afraid. It doesn't give errors , but had no impact. I suspect I'm not applying it correctly. I'll attach a sample file for you to see. I've removed all but the most relevant data , but do let me know if you need more.

    Thanks again.

  35. #35
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,524

    Re: Extracting unique values from a column

    Are the Passwords for all sheet common?

  36. #36
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,524

    Re: Extracting unique values from a column

    1) Delete the current codes in sheet 2017 - 2021 code module.
    2) Double click on "ThisWorkbook" in Project pane on the left and paste the following code.
    Please Login or Register  to view this content.

  37. #37
    Forum Contributor
    Join Date
    01-18-2013
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    641

    Re: Extracting unique values from a column

    Hi

    ok thanks very much for this. This now generates the unique list in the 'Unique values' sheet. Unfortunately , it has stopped updating lists in column O on the other sheets.

    I do also find 'protected sheet' errors when trying to enter dates in sheets 2017-2021.

    I'll attach a fuller sheet with the code above in place so it's clearer..

    Grateful for you advice.

  38. #38
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,524

    Re: Extracting unique values from a column

    OK, hope this works as you expected.
    Please Login or Register  to view this content.

  39. #39
    Forum Contributor
    Join Date
    01-18-2013
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    641

    Re: Extracting unique values from a column

    Yes - excellent. Thanks Jindon. You are clearly very expert.

    I did try to add a count of occurrences for each name in column b of the Unique values sheet

    {=SUM(COUNTIF(INDIRECT(ROW($2017:$2021)&"!F3:F500"),B3))}

    It works , but it trips the routine up. Never mind.

    I wonder if a simple macro activated on opening the Unique values sheet would be the answer after all. It would list the unique names from B3 and count occurrences or each from c3.

    Anyway , I'm really grateful for your time and expertise.

  40. #40
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,524

    Re: Extracting unique values from a column

    OK, I shut down my pc already, so will add the code for it tomorrow.

  41. #41
    Forum Contributor
    Join Date
    01-18-2013
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    641

    Re: Extracting unique values from a column

    Ok thanks Jindon.


  42. #42
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,524

    Re: Extracting unique values from a column

    No formula, simple loop with dictionary object to count...
    Please Login or Register  to view this content.

  43. #43
    Forum Contributor
    Join Date
    01-18-2013
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    641

    Re: Extracting unique values from a column

    Excellent - working perfectly now. Thanks.

  44. #44
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,524

    Re: Extracting unique values from a column

    You are welcome and thanks for the reps.

+ 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. [SOLVED] Formula extracting unique column values
    By labogola in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 09-10-2013, 02:34 AM
  2. Replies: 0
    Last Post: 02-20-2012, 02:06 PM
  3. Replies: 11
    Last Post: 02-17-2012, 11:26 AM
  4. Replies: 4
    Last Post: 01-26-2011, 08:38 AM
  5. Populate a column by extracting unique values from another column?
    By Duke Carey in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 09-06-2005, 07:05 AM
  6. Populate a column by extracting unique values from another column?
    By Duke Carey in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 09-06-2005, 06:05 AM
  7. Populate a column by extracting unique values from another column?
    By Mike Palmer in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-06-2005, 12:05 AM
  8. Populate a column by extracting unique values from another column?
    By Mike Palmer in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 06-10-2005, 11:05 AM

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