VBScript Dynamic Array implementation!

A small recording method, on VBScript, dynamic Array, and is also applicable to the VBA,

A long time ago, when writing VBA, I felt Array and inconvenient to use, because the fixed size,
was thinking that if the Array can be like Python in the list handy nice ah,
then the following, on a recording method, let Array become dynamic, and easy to use!

Implementation:

In the example below, to set out an empty Array,
then, the following method of dynamic Array,
and, the numbers 1 to 10, was added to a Array go.

'动态 Array 实现
myArray = Array()
For i = 1 To 10
    ReDim Preserve myArray(UBound(myArray) + 1)
    myArray(UBound(myArray)) = i
Next

Dynamic Array then so happy to achieve it, (^ _-) ☆

Lower contrast and Python list of bar code, the feeling is not like it.

# Python 中 list 的使用
myList = list()
for i in range(10):
    myList.append(i)
print(myList)

Data output:

Then, thinking the next issue, but also a problem when I write VBA consider before,
that is, how all of a sudden, to see all the data in the Array,
before this method is to use the For Loop, the data in the Array Print out one by one ,
but now I found a simple method, as follows:

'最简单的方法:
MsgBox Join (myArray, vblf)

'之前使用的笨方法:
For Each i In myArray
    Debug.Print i
Next

Conclusion:

Dynamic Array above method, are available in VBScript and VBA!



Guess you like

Origin www.cnblogs.com/bitssea/p/12625080.html