Excel Pragma

February 3, 2005

Concatenate with Separators

Filed under: Data Analysis, General, User-Defined Functions — Admin @ 7:53 am

Ever needed the values from a range of cells as a string separated by commas ? I used do this in a two step concatenate : the first step is for every cell in the range and then another concatenate for all the “concatenated cells”.

So today, I decided to create my own UDF to generate a string from a selection of cells and any “separator”.

Here’s the ConcSep function :

Public Function ConcSep(InCells As Range, Sep As String) As String
    Dim OutStr As String
    Dim Cell As Variant
    
    OutStr = “”
    If InCells Is Nothing Then Exit Function
    On Error Resume Next
    For Each Cell In InCells
        OutStr = OutStr & Cell.Value & Sep
    Next Cell
    OutStr = Left(OutStr, Len(OutStr) - Len(Sep))
    ConcSep = OutStr
End Function

And here’s the output :

ConcSep

 

Obviously, this now goes into the add-in you created . and will also be part of my Utilities project.

No Comments

No comments yet.

RSS feed for comments on this post.

Sorry, the comment form is closed at this time.

Powered by WordPress