SoFunction
Updated on 2025-03-10

VBScript Dynamic Array implementation code

Record a small method about the implementation of dynamic Array in VBScript, which also applies to VBA.

A long time ago, when I was writing VBA, I felt that using Array was inconvenient because the size was fixed.
What I thought at the time was that it would be great if Array could be as useful as the list in Python.
So below, let’s record a method that can make Array dynamic and easy to use!

Implementation method:

In the following example, first set an empty Array to come out.
Then use the following method to implement dynamic Array,
And, add the numbers 1 to 10 and one to the Array.

'Dynamic Array implementation

myArray = Array()
For i = 1 To 10
  ReDim Preserve myArray(UBound(myArray) + 1)
  myArray(UBound(myArray)) = i
Next

Then dynamic Array is so happy to realize, (^_−)☆

Compare the code with Python list, does it feel very similar?

# Use of list in PythonmyList = list()
for i in range(10):
  (i)
print(myList)

Data output:

Then, thinking about the next question is also the question I considered when I wrote VBA before.
That is, how can I see all the data in Array all at once?
The previous method was to use For Loop to print the data in Array one by one.
But now I have found a simple method, the code is as follows:

'The easiest way:

MsgBox Join (myArray, vblf)

'The stupid method I used before:
For Each i In myArray
   i
Next

Conclusion:

The dynamic Array method above is available in both VBScript and VBA!

Summarize

This is the article about VBScript Dynamic Array implementation code. For more related VBScript Dynamic Array content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!