Return all non duplicates from a range

Pumper

Board Regular
Joined
Sep 12, 2013
Messages
114
Office Version
  1. 365
Hi All,

I would like to return all values from a range that are not duplicated (not unique values just those appearing once in the range).

The length of the columns my vary and each column can be deferent lengths.

Either a formula or VBA would work, have seen some other VBA but they delete the entire row.
Random example here:
Before: Sheet1
1727935360357.png


Desired Result : Sheet2
1727935384327.png


Any assistance on this would be much appreciated

Thanks
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Are you still using Excel 2016? If you happen to have 365 then you can do it like this.

24 10 03.xlsm
ABCD
1AppleOrangeApple
2OrangePearBanana
3PearBanana
4OrangeOrange
5Pear
Non-dupes
Cell Formulas
RangeFormula
D1:D2D1=UNIQUE(TOCOL(A1:B5,1),,1)
Dynamic array formulas.
 
Upvote 1
Solution
And if you really want a VBA Option give this a try.

VBA Code:
Sub GetUniqueValues()

    Dim shtSrc As Worksheet, shtDest As Worksheet
    Dim rngSrc As Range, rngDest As Range
    Dim arrSrc As Variant, arrOut As Variant
    Dim dictSrc As Object, dictKey As String, vKey As Variant
    Dim i As Long, j As Long, iOut As Long
    
    Set shtSrc = Worksheets("Sheet1")
    Set shtDest = Worksheets("Sheet2")
    
    Set rngSrc = shtSrc.Range("A1").CurrentRegion
    arrSrc = rngSrc.Value
    
    Set rngDest = shtDest.Range("A1")
    rngDest.CurrentRegion.ClearContents
    
    Set dictSrc = CreateObject("Scripting.dictionary")
    dictSrc.CompareMode = vbTextCompare
    
    ' Load details range into Dictionary
    For j = 1 To UBound(arrSrc, 2)
        For i = 1 To UBound(arrSrc, 1)
            dictKey = arrSrc(i, j)
            
            If dictKey <> "" Then
                If Not dictSrc.exists(dictKey) Then
                    dictSrc(dictKey) = 1
                Else
                    dictSrc(dictKey) = dictSrc(dictKey) + 1
                End If
            End If
        Next i
    Next j
        
    ' Load distinct unique values into output array
    ReDim arrOut(1 To dictSrc.Count, 1 To 1)
        
    For Each vKey In dictSrc.keys
        If dictSrc(vKey) = 1 Then
            iOut = iOut + 1
            arrOut(iOut, 1) = vKey
        End If
    Next vKey
    
    ' Output results
    rngDest.Resize(iOut).Value = arrOut
End Sub
 
Upvote 1
Hi All,

I would like to return all values from a range that are not duplicated (not unique values just those appearing once in the range).

The length of the columns my vary and each column can be deferent lengths.

Either a formula or VBA would work, have seen some other VBA but they delete the entire row.
Random example here:
Before: Sheet1
View attachment 117629

Desired Result : Sheet2
View attachment 117630

Any assistance on this would be much appreciated

Thanks
Another option that assumes that there is space to the right of the block of values.

VBA Code:
Public Sub subGetUniqueValues()
Dim rng As Range
Dim i As Integer
Dim rngFor As Range
  
  Sheets("Sheet2").Range("A:A").Cells.ClearContents

  With Sheets("Sheet1").Range("A1").CurrentRegion
    
    Set rngFor = .Offset(0, .Columns.Count + 2)
    
    rngFor.Formula2 = "=COUNTIF(" & .Address & "," & _
      .Cells(1).Address(0, 0) & ")"
     
    For Each rng In rngFor
      If rng.Value = 1 Then
        Sheets("Sheet2").Cells(i + 1, 1).Value = _
          rng.Offset(0, -2 + (-.Columns.Count)).Value
        i = i + 1
      End If
    Next rng
    
  End With
  
  rngFor.Clear
      
End Sub
 
Upvote 1
Thanks all for taking the time to look at this! (sorry late reply, assume different time zones)

Peter that worked perfectly, even though I am still on 2016, so thank you very much.

Think in this instance I will go with the formula as I have a bunch of other macros proceeding this but will definitely save those for future reference.
 
Upvote 0
Peter that worked perfectly, even though I am still on 2016, so thank you very much.
If my formula worked for you then I don't believe that you are using Excel 2016 since the TOCOL function is only available in Microsoft 365 and excel for the web ...

1728029477790.png



.. and the UNIQUE function is also not available in Excel 2016:

1728029561481.png


If I go to File -> Account this is what I get. What is it for you? .. or are you using Excel for the web?

1728029749790.png
 
Upvote 0
If my formula worked for you then I don't believe that you are using Excel 2016 since the TOCOL function is only available in Microsoft 365 and excel for the web ...

View attachment 117679


.. and the UNIQUE function is also not available in Excel 2016:

View attachment 117680

If I go to File -> Account this is what I get. What is it for you? .. or are you using Excel for the web?

View attachment 117681
Well there you go...I just asked our IT department and we switched over about a month ago so as per every other time you have helped Peter you are correct ;) same as what you have above.

Thanks again!
 
Upvote 0
Well there you go...I just asked our IT department and we switched over about a month ago so as per every other time you have helped Peter you are correct ;) same as what you have above.

Thanks again!
Cheers. :)
.. and I note that you have updated your profile to reflect your actual current version. (y)
 
Upvote 0

Forum statistics

Threads
1,224,809
Messages
6,181,075
Members
453,020
Latest member
mattg2448

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top