I’ve found out how to create an array, but I cannot push items easily. I must maintain a index to the next position and increment every time I push an item;
I’ve also found the collection which has the nice .Add method that acts exactly like a push method. But how I join them? The global Join method doesn’t work with Collections.
What I’m missing here? anybody could help me to define an array, push items easily without an index and then output them to a string spaced by “, “?
Thanks
You can’t do that directly. Arrays in VBA usually need to be indexed and dimensioned before use.
You can use a dynamic array and resize before assigning a variable:
The preserve key word maintains the values in the array rather than overwriting them. The above approach is generally not recommended however since Preserve is costly and only allows you to resize the last dimension of an array.
Collections are different, are slower and generally less flexible in a VBA environment (you haven’t said which environment, but I’ll assume Excel)
You need to loop through them to build an array.
There are a numerous of other options depending on your needs
Built in method
for strings, have a look at split:
Using External objects
Scripting Dictionary
.Net arrayList