Is there a way to join a collection in VBA? I can find join(array, ";"), but this function can not be applied to a collection.
Thanks.
You can use the optional Before and After arguments to maintain an ordered collection of objects. The item to be added is placed in the collection before or after the item identified by the Before or After argument, respectively.
To get started with collection first, we need to declare the variable as “Collection.” Since the collection is an object variable, we need to set the object reference by creating a new instance. Now with the variable, we can access all the methods of the collection variable “Col.”
The VBA Join function joins together an array of substrings and returns a single string. The array of substrings that you want to join together. The delimiter that is used to separate each of the substrings when making up the new string. If omitted, the [Delimiter] is set to be a space " ".
In this article A Collection object is an ordered set of items that can be referred to as a unit.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With