Open In App

How to Convert VBA Collections to Array in Excel?

Last Updated : 16 Oct, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

An object that can store a number of values whether it can be of string data type or integer which can be easily manipulated or iterated is called Collection. On the other hand, Array is also used to store the data but it is multidimensional but collections are single dimensions. Now, we will see how to convert the collection to an array for that we have to follow further steps:

Converting Excel VBA Collection to Array

Step 1: Press Alt + F11 to get the VBA box and select Insert and Module to write the code.

Inserting-module

 

Step 2: Define a sub-procedure in VBE.

Defining-sub-procedure-in-VBA

 

Step 3: Declare two variables one with the name “collection” as New Collection and another one with the name “arr” of size 3 type String.

Declaring-variables

 

Step 4: Now, we will add items to the collection with the help of the “Add” keyword. 

Adding-items-to-the-collection

 

Step 5: Adding items to the array from the collection using each loop.

Adding-items-to-array

 

Step 6: Use MsgBox to print the elements in the array.

Using-MsgBox-to-print-elements

 

Step 7: Press F5 to see the output.

Output1

 

Output2

 

Output3

 


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads