Extract unique values across the same row

YasserKhalil
PlatinumLounger
Posts: 4931
Joined: 31 Aug 2016, 09:02

Extract unique values across the same row

Post by YasserKhalil »

Hello everyone

I have data in range("A1:E4") ..Row 1 contain the headers and column A contains the items
And the values which needed to be manipulated in range("B2:E5")

Need to extract unique values across the same row ..
For example : In B2:E2 ( 555,555,55,"") would be ( 555,55,"","")
And so on for other rows

Desired output put for guide and more clarification
There is already a code that uses VBA arrays but doesn't work on unique .. I need to adapt it to my requirement if possible
Thanks advanced for help
You do not have the required permissions to view the files attached to this post.

User avatar
HansV
Administrator
Posts: 78549
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Extract unique values across the same row

Post by HansV »

Welcome to Eileen's Lounge!

Here is a version that works with the cells themselves:

Code: Select all

Sub Test2()
    Dim rng As Range
    Dim m As Long
    Dim n As Long
    Dim r As Long
    Dim c As Long
    Dim i As Long
    Dim col As Collection
    Application.ScreenUpdating = False
    Range("J1").Value = Range("A1").Value
    Set rng = Range("A1").CurrentRegion
    m = rng.Rows.Count
    n = rng.Columns.Count
    For r = 2 To m
        Set col = New Collection
        On Error Resume Next
        For c = 1 To n
            If Cells(r, c).Value <> "" Then
                col.Add Item:=Cells(r, c).Value, Key:=CStr(Cells(r, c).Value)
            End If
        Next c
        On Error GoTo 0
        For c = 1 To col.Count
            Cells(r, c + 9).Value = col(c)
        Next c
    Next r
    n = Range("J1").CurrentRegion.Columns.Count
    For c = 1 To n - 1
        Cells(1, c + 10).Value = c
    Next c
    Application.ScreenUpdating = True
End Sub
Best wishes,
Hans

YasserKhalil
PlatinumLounger
Posts: 4931
Joined: 31 Aug 2016, 09:02

Re: Extract unique values across the same row

Post by YasserKhalil »

That's amazing Mr. Hans
Thank you very much for this awesome solution

Best Regards

User avatar
HansV
Administrator
Posts: 78549
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Extract unique values across the same row

Post by HansV »

Well, it's based on your macro. The main difference is that it fills the new range row by row.
Best wishes,
Hans