Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Filtering Columns for Unique Values.

Filtering Columns for Unique Values

Written by Allen Wyatt (last updated May 23, 2023)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365


2

It is not unusual to acquire or develop data tables that have duplicate values in a column. If you want to see only the unique values, without the duplicates, you want to filter your data table. Excel makes this rather easy for most scenarios. For instance, let's say you have a data table in which you have part numbers in column A. If you want to filter the list so you see only unique part numbers, you can follow these steps:

  1. Make sure there are column headings at the top of your list of part numbers.
  2. Select one of the cells in the list of part numbers.
  3. Display the Data tab of the ribbon.
  4. Click the Advanced Filter tool (Excel 2007) or the Advanced tool (later versions of Excel) in the Sort & Filter group. Excel displays the Advanced Filter dialog box. (See Figure 1.)
  5. Figure 1. The Advanced Filter dialog box.

  6. I always like to choose the Copy to Another Location option.
  7. In the Copy To field, specify the cell where you want the list of unique, filtered values to be copied.
  8. Make sure the Unique Records Only check box is selected.
  9. Click on OK.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (8732) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365. You can find a version of this tip for the older menu interface of Excel here: Filtering Columns for Unique Values.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Setting Gap Spacing in the Equation Editor

The Equation Editor is a great tool that allows you to add equations to your document. You have quite a bit of control ...

Discover More

Using Cross-References in Footnotes

Need to make a cross-reference from one footnote to another footnote? You can do it if you throw bookmarks into the mix, ...

Discover More

Enhancing Word Documents with Dynamic Fields (Table of Contents)

Add a field to your document and you add dynamic content. Word provides a wide variety of fields that can be used in a ...

Discover More

Save Time and Supercharge Excel! Automate virtually any routine task and save yourself hours, days, maybe even weeks. Then, learn how to make Excel do things you thought were simply impossible! Mastering advanced Excel macros has never been easier. Check out Excel 2010 VBA and Macros today!

More ExcelTips (ribbon)

Advanced Filtering

Many people know how to use AutoFilter, but there are times when you need some more filtering muscle. Here's how you can ...

Discover More

Extracting Targeted Records from a List

If you have a bunch of data in an Excel worksheet, you may need to work with just a subset of that data. One way to do ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 2 + 8?

2019-03-19 06:31:47

Thomas Papavasileiou

In cases where the extraction of unique values is used often, I wrote a macro that fulfills the task.
Feel free to use it.

Sub Extract_uniques_to_right_1()

On Error GoTo errorhandling

'Variables definition
Dim ad1 As String
Dim ad2 As String
Dim ad3 As String
Dim msg As String
Dim ans As String
Dim ord_ As String

Dim r1 As Double
Dim r2 As Double
Dim lr As Double

Dim so_c As Integer
Dim so_r As Integer
Dim c1 As Integer
Dim c2 As Integer
Dim c3 As Integer

'Tables or other objects existence control
If ActiveSheet.ListObjects.Count > 0 Then
msg = "Sheet may contain table/s." & vbCr & vbCr & "Macro ends"
MsgBox msg, vbOKOnly + vbInformation, "Oops!"
Exit Sub
End If

'Input dialog and input address
If Selection.Cells.Count <> 1 Then
msg = "Select top label cell to extract unique records"
ad1 = Application.InputBox(msg, Default:=Selection.Address, Type:=8).Address
Else
ad1 = Selection.Address
End If

'Controls. Empty cell selection control
If IsEmpty(Range(ad1)) Then
msg = "Empty cell selected. Macro ends. "
MsgBox msg, vbOKOnly + vbInformation
Exit Sub
End If

'Controls. Multiple cells selection control
If Range(ad1).Cells.Count > 1 Then
msg = "Selection points to more than one cells. Macro ends. "
MsgBox msg, vbOKOnly + vbInformation
Exit Sub
End If

'Range data collection part
With Range(ad1)
r1 = .Row
c1 = .Column
End With

With Range(ad1).CurrentRegion
r2 = .Rows.Count + r1 - 1
c3 = .Column
c2 = .Columns.Count + c3 - 1
lr = .Row + .CurrentRegion.Rows.Count - 1
End With

'Controls. Last row selection control
If r1 = lr Then
msg = "Selection points to last cell of a region. Macro ends. "
MsgBox msg, vbOKOnly + vbInformation
Exit Sub
End If

'Controls. Sheet's columns limit control
If c2 > 255 Then
msg = "Sheets column limits exceeded. Macro ends. "
MsgBox msg, vbOKOnly + vbInformation
Exit Sub
End If

'Setting various application elements to make macro run faster
With Application
.ScreenUpdating = False
.Calculation = xlCalculationManual
End With

'Copying header to extract area
Range(ad1).Copy Destination:=Range(ad1).Offset(0, c2 - c1 + 1)

'Main part of the macro
Range(Cells(r1, c1), Cells(r2, c1)).AdvancedFilter _
Action:=xlFilterCopy, _
CriteriaRange:=Cells(r1, c2 + 2).Range("a1:a2"), _
CopyToRange:=Cells(r1, c2 + 1), _
Unique:=True

'Adding "U " as "Unique" to header and setting focus on this cell
msg = Range(ad1).Offset(0, c2 - c1 + 1).Value
Range(ad1).Offset(0, c2 - c1 + 1).Value = "U " & msg
Range(ad1).Offset(0, c2 - c1 + 1).Select

'Resetting various application elements
With Application
.ScreenUpdating = True
.Calculation = xlCalculationAutomatic
.StatusBar = False
End With

'Sort dialog and action
msg = "Do you want to sort extracted data? " + Chr(13) + Chr(13)
msg = msg + "Yes for ascending" + Chr(13)
msg = msg + "No for descending" + Chr(13)
msg = msg + "Cancel for unsorted"

ans = MsgBox(msg, vbYesNoCancel + vbQuestion)

If ans = 6 Or ans = 7 Then
If ans = 6 Then
ord_ = xlAscending
Else
ord_ = xlDescending
End If

so_c = Selection.Column
so_r = Selection.Row
ad3 = Range(Cells(so_r, so_c), Cells(lr, so_c)).Address

Range(ad3).Sort _
key1:=Cells(so_r, so_c), _
order1:=ord_, _
header:=xlYes, _
OrderCustom:=1, _
MatchCase:=False, _
Orientation:=xlTopToBottom
End If

Exit Sub

errorhandling:
With Application
.DisplayAlerts = True
.ScreenUpdating = True
.StatusBar = False
.Calculation = xlCalculationAutomatic
End With

'This part handles the "Cancel" button selection of the first dialog
If IsEmpty(ad1) Then
msg = "Cancel selected. Macro ends. "
MsgBox msg, vbOKOnly + vbInformation
Exit Sub
End If

'This part handles other errors if any appears
msg = "Macro encountered an unexpected error and halts" + Chr$(13)
msg = msg + "Please try to re-run checking initial conditions" + Chr$(13) + Chr$(13)
msg = msg + "If the error persists contact the programmer" + Chr$(13)
msg = msg + "thpapavasiliou@yahoo.com"
MsgBox msg, vbOKOnly + vbCritical

End Sub


2019-03-16 20:42:43

Andy

Coming soon to Office365 is the UNIQUE function, which is an alternative method to getting a list of unique part numbers. See https://support.office.com/en-us/article/unique-function-c5ab87fd-30a3-4ce9-9d1a-40204fb85e1e or https://www.youtube.com/watch?v=vy8y4CN-IHY for its use. I am not sure if it can return the whole table though.

One benefit is that it will update the list of unique part numbers as a new one is added to the table.


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.