0

I have a column that contains mixed strings and I need to find all the unique strings and declare to either a string or array variable. The last row of the column will vary so I cannot use a definite range. I was thinking of using some form of string comparison of the preceding cell and current cell, but like I said the data is mixed so when redundant data comes up this complicates the problem. Here is a picture to try and explain it better. EDIT: The string concatenation I will worry about that later.

enter image description here

1 Answer 1

1

Using Excel 365.

With data in A1 through A11, in another cell enter:

=UNIQUE(A1:A11)

to get:

enter image description here

or:

=TEXTJOIN(",",TRUE,UNIQUE(A1:A11))

to get a comma-separated list:

enter image description here

EDIT#1:

With VBA, try this UDF:

Public Function unikue(rng As Range)
    Dim arr, c As Collection, r As Range
    Dim nCall As Long, nColl As Long
    Dim i As Long
    Set c = New Collection
    
    nCall = Application.Caller.Count
    
    On Error Resume Next
        For Each r In rng
            c.Add r.Text, CStr(r.Text)
        Next r
    On Error GoTo 0
    nColl = c.Count
    
    
    If nCall > nColl Then
        ReDim arr(1 To nCall, 1 To 1)
        For i = 1 To nCall
            arr(i, 1) = ""
        Next i
    Else
        ReDim arr(1 To nColl, 1 To 1)
    End If
    
    For i = 1 To nColl
        arr(i, 1) = c.Item(i)
    Next i
    
    unikue = arr
End Function
Sign up to request clarification or add additional context in comments.

2 Comments

@ian.templeton see my EDIT#1
No problem doing this in VBA as the above Excel formula result can be written to any cell using VBA via Evaluate() (not forgetting double quotations marks within the string to be evaluated:-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.