Filter duplicates in a large dataset [UDF]
This article demonstrates a user defined function that extracts duplicate values and also count duplicates.
Example, the image below shows a list containing duplicate values.
User defined function
Function DuplicateValues(rng As Variant, Optional CountDuplicates As Variant) As Variant Dim Test As New Collection Dim Dupes As New Collection Dim Value As Variant Dim Item As Variant Dim temp() As Variant ReDim temp(0) rng = rng.Value If IsMissing(CountDuplicates) Then CountDuplicates = False On Error Resume Next For Each Value In rng If Len(Value) > 0 Then Test.Add Value, CStr(Value) If Err Then If Len(Value) > 0 Then Dupes.Add Value, CStr(Value) Err = False End If Next Value On Error GoTo 0 If CountDuplicates = False Then For Each Item In Dupes temp(UBound(temp)) = Item ReDim Preserve temp(UBound(temp) + 1) Next Item DuplicateValues = Application.Transpose(temp) Else DuplicateValues = Dupes.Count End If End Function
How to add the user defined function to your workbook
- Press Alt-F11 to open the Visual Basic editor
- Press with left mouse button on Module on the Insert menu
- Copy and paste the above user defined function
- Exit visual basic editor
How to extract duplicate values
- Select a cell range
- Type =DuplicateValues($A$1:$F$3000, FALSE)
- Press and hold CTRL + SHIFT simultaneously
- Press Enter once
- Release all keys
How to count duplicate values
- Select a cell
- Type =DuplicateValues($A$1:$F$3000, TRUE) into formula bar and press ENTER.
User defined function category
This article demonstrates two ways to calculate the number of times each word appears in a given range of cells. […]
This article demonstrates how to convert a range of cells containing strings separated by a delimiter into a range of […]
This article demonstrates a User Defined Function (UDF) that counts unique distinct cell values based on a given cell color. […]
Excel categories
Leave a Reply
How to comment
How to add a formula to your comment
<code>Insert your formula here.</code>
Convert less than and larger than signs
Use html character entities instead of less than and larger than signs.
< becomes < and > becomes >
How to add VBA code to your comment
[vb 1="vbnet" language=","]
Put your VBA code here.
[/vb]
How to add a picture to your comment:
Upload picture to postimage.org or imgur
Paste image link to your comment.