Home » excel » excel – Create unique sequence for row

excel – Create unique sequence for row

Posted by: admin May 14, 2020 Leave a comment

Questions:

I have a few thousand rows of data and need to create the unique sequences for each. Here is an example (each value is in its own cell):

Text1| Text2| Text2| Text2| Text3| Text3| Text1|

I need to create the unique sequence to look sometime like this

Text1| Text2| Text3| Text1|    or
Text1,Text2,Text3,Text1|

Any ideas?

How to&Answers:

If the first Text1 is in A1, then:

=A1&","&IF(A1<>B1,B1&",","")&IF(B1<>C1,C1&",","")&IF(C1<>D1,D1&",","")&IF(D1<>E1,E1&",","")&IF(E1<>F1,F1&",","")&IF(F1<>G1,G1,"")  

might be worth a try.

Answer:

As a user defined function

Public Function ClarkeyCat2(ByRef rng As Range, delimIT As String, Optional ByVal trimIT As Boolean = False) As Variant
Dim c As Range
Dim UniqueColl As Collection
Dim tmpValue As Variant
Set UniqueColl = New Collection


If Not trimIT Then

    On Error Resume Next
    For Each c In rng
        If (c.Value <> "") Then
            UniqueColl.Add c.Value, c.Value
        End If
    Next

    For k = 1 To UniqueColl.Count
        ClarkeyCat2 = ClarkeyCat2 & IIf(ClarkeyCat2 = "", "", delimIT) & UniqueColl.Item(k)
    Next
Else
    On Error Resume Next
    For Each c In rng
        If (c.Value <> "") Then
            tmpValue = Trim(c.Value)
            UniqueColl.Add tmpValue, tmpValue
        End If
    Next

    For k = 1 To UniqueColl.Count
        ClarkeyCat2 = ClarkeyCat2 & IIf(ClarkeyCat2 = "", "", delimIT) & UniqueColl.Item(k)
    Next
End If

End Function

Use like this

enter image description here

Update Updated to optionally strip extraneous white space from cell entries, before deciding if entries are unique. Default behaviour is to not strip white space. Set using the optional Boolean third parameter.