How to join a collection in VBA

This is how to join it:

Join(CollectionToArray(colData), ",")

And the function:

Public Function CollectionToArray(myCol As Collection) As Variant

    Dim result  As Variant
    Dim cnt     As Long

    ReDim result(myCol.Count - 1)

    For cnt = 0 To myCol.Count - 1
        result(cnt) = myCol(cnt + 1)
    Next cnt

    CollectionToArray = result

End Function

Unfortunately, no, there's nothing built-in.

You'll have to either

  • convert the collection to an array (no built-in for that either, you'll have to loop through all the items) and then use Join(array, ";") or

  • join your collection "the hard way" (set first flag, loop through items, add ";" if not first, clear first, add item).