Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to join a collection in VBA

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.

like image 820
Benj Avatar asked Mar 12 '15 16:03

Benj


People also ask

How do you add items to a collection in VBA?

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.

How do I use collections in Excel VBA?

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.”

How do I join an array in VBA?

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 " ".

What is a collection object in VBA?

In this article A Collection object is an ordered set of items that can be referred to as a unit.


1 Answers

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).

like image 175
Heinzi Avatar answered Oct 21 '22 00:10

Heinzi