Getting unique values in Excel by using formulas only
Ok, I have two ideas for you. Hopefully one of them will get you where you need to go. Note that the first one ignores the request to do this as a formula since that solution is not pretty. I figured I make sure the easy way really wouldn't work for you ;^).
Use the Advanced Filter command
- Select the list (or put your selection anywhere inside the list and click ok if the dialog comes up complaining that Excel does not know if your list contains headers or not)
- Choose Data/Advanced Filter
- Choose either "Filter the list, in-place" or "Copy to another location"
- Click "Unique records only"
- Click ok
- You are done. A unique list is created either in place or at a new location. Note that you can record this action to create a one line VBA script to do this which could then possible be generalized to work in other situations for you (e.g. without the manual steps listed above).
Using Formulas (note that I'm building on Locksfree solution to end up with a list with no holes)
This solution will work with the following caveats:
Here is the summary of the solution:
- For each item in the list, calculate the number of duplicates above it.
- For each place in the unique list, calculate the index of the next unique item.
- Finally, use the indexes to create a new list with only unique items.
And here is a step by step example:
- Open a new spreadsheet
- In a1:a6 enter the example given in the original question ("red", "blue", "red", "green", "blue", "black")
- Sort the list: put the selection in the list and choose the sort command.
- In column B, calculate the duplicates:
- In B1, enter "=IF(COUNTIF($A$1:A1,A1) = 1,0,COUNTIF(A1:$A$6,A1))". Note that the "$" in the cell references are very important as it will make the next step (populating the rest of the column) much easier. The "$" indicates an absolute reference so that when the cell content is copy/pasted the reference will not update (as opposed to a relative reference which will update).
- Use smart copy to populate the rest of column B: Select B1. Move your mouse over the black square in the lower right hand corner of the selection. Click and drag down to the bottom of the list (B6). When you release, the formula will be copied into B2:B6 with the relative references updated.
- The value of B1:B6 should now be "0,0,1,0,0,1". Notice that the "1" entries indicate duplicates.
- In Column C, create an index of unique items:
- In C1, enter "=Row()". You really just want C1 = 1 but using Row() means this solution will work even if the list does not start in row 1.
- In C2, enter "=IF(C1+1<=ROW($B$6), C1+1+INDEX($B$1:$B$6,C1+1),C1+1)". The "if" is being used to stop a #REF from being produced when the index reaches the end of the list.
- Use smart copy to populate C3:C6.
- The value of C1:C6 should be "1,2,4,5,7,8"
- In column D, create the new unique list:
- In D1, enter "=IF(C1<=ROW($A$6), INDEX($A$1:$A$6,C1), "")". And, the "if" is being used to stop the #REF case when the index goes beyond the end of the list.
- Use smart copy to populate D2:D6.
- The values of D1:D6 should now be "black","blue","green","red","","".
Hope this helps....
Do you know a way in Excel to "calculate" by formula a list of unique values ?
E.g: a range contains values
and I want to have as result
"black" + eventually 2 other blank cells.
I already found a way to get a calculated sorted list using SMALL or LARGE combined with INDEX, but I'd like to have this calculated sort as well, WITHOUT USING VBA.
Select the column with duplicate values then go to Data Tab, Then Data Tools select remove duplicate select 1) "Continue with the current selection" 2) Click on Remove duplicate.... button 3) Click "Select All" button 4) Click OK
now you get the unique value list.
Drew Sherman's solution is very good, but the list must be contiguous (he suggests manually sorting, and that is not acceptable for me). Guitarthrower's solution is kinda slow if the number of items is large and don't respects the order of the original list: it outputs a sorted list regardless.
I wanted the original order of the items (that were sorted by the date in another column), and additionally I wanted to exclude an item from the final list not only if it was duplicated, but also for a variety of other reasons.
My solution is an improvement on Drew Sherman's solution. Likewise, this solution uses 2 columns for intermediate calculations:
The list with duplicates and maybe blanks that you want to filter. I will position it in the A11:A1100 interval as an example, because I had trouble moving the Drew Sherman's solution to situations where it didn't start in the first line.
This formula will output 0 if the value in this line is valid (contains a non-duplicated value). Note that you can add any other exclusion conditions that you want in the first IF, or as yet another outer IF.
Use smart copy to populate the column.
In the first line we will find the first valid line:
From that position, we search for the next valid value with the following formula:
Put it in the second line and use smart copy to fill the rest of the column. This formula will output #N/D error when there is no more unique itens to point. We will take advantage of this in the next column.
Now we just have to get the values pointed by column C:
=IFERROR(INDEX($A$11:$A$1100; C11); "")
Use smart copy to populate the column. This is the output unique list.
Try this formula in
F2 and press
A roundabout way is to load your Excel spreadsheet into a Google spreadsheet, use Google's UNIQUE(range) function - which does exactly what you want - and then save the Google spreadsheet back to Excel format.
I admit this isn't a viable solution for Excel users, but this approach is useful for anyone who wants the functionality and is able to use a Google spreadsheet.
If one puts all the data in the same columns and uses the following formula
=IF(C105=C104,"Duplicate","Not a Duplicate")
- Sort the data
- Add column for the formula
- Checks if the cell equals the cell above it
- Then filter
Not a Duplicate
- Optional: Copy the data calculated by the formula column and paste as values only (that way if you start deleting data, you don't start to get errors
- NOTE/WARNING: This only works if you sort the data first
=IF(C105=C104,"Duplicate","Not a Duplicate")
This only works if the values are in order i.e all the "red" are together and all the "blue" are together etc. assume that your data is in column A starting in A2 - (Don't start from row 1) In the B2 type in 1 In b3 type =if(A2 = A3, B2,B2+1) Drag down the formula until the end of your data All " Red" will be 1 , all "blue" will be 2 all "green" will be 3 etc.
In C2 type in 1, 2 ,3 etc going down the column In D2 = OFFSET($A$1,MATCH(c2,$B$2:$B$x,0),0) - where x is the last cell Drag down, only the unique values will appear. -- put in some error checking
Resorting to a PivotTable might not count as using formulas only but seems more practical that most other suggestions so far:
I'm surprised this solution hasn't come up yet. I think it's one of the easiest
Give your data a heading and put it into a dynamic named range (i.e. if your data is in col
And then create a pivot table, making the source your named range.
Simply putting the heading into the rows section and you'll have the unique values, sort any way you like with the inbuilt feature.
Assuming Column A contains the values you want to find single unique instance of, and has a Heading row I used the following formula. If you wanted it to scale with an unpredictable number of rows, you could replace A772 (where my data ended) with =ADDRESS(COUNTA(A:A),1).
This will display the unique value at the LAST instance of each value in the column and doesn't assume any sorting. It takes advantage of the lack of absolutes to essentially have a decreasing "sliding window" of data to count. When the countif in the reduced window is equal to 1, then that row is the last instance of that value in the column.