How do I declare an array variable in VBA?

后端 未结 7 691
灰色年华
灰色年华 2020-12-08 20:02

I need to add the var in array

Public Sub Testprog()

Dim test As Variant
Dim iCounter As Integer

If test = Empty Then
    iCounter = 0
    test(iCounter) =         


        
7条回答
  •  遥遥无期
    2020-12-08 20:50

    Further to Cody Gray's answer, there's a third way (everything there applies her as well):

    You can also use a dynamic array that's resized on the fly:

    Dim test() as String
    Dim arraySize as Integer
    
    Do While someCondition
        '...whatever
        arraySize = arraySize + 1
        ReDim Preserve test(arraySize)
        test(arraySize) = newStringValue
    Loop
    

    Note the Preserve keyword. Without it, redimensioning an array also initializes all the elements.

提交回复
热议问题