vba

Declare array of Integers


I want to create an array of integers in VBA

I know that I can do using

Dim xyz() as Variant
xyz = Array(1,3,5,10,20,30,32, 45, 76, 89, 100, 34, 12, 0, 1, 100, 34, 45, 56, 67)

but I do not want to use variant data type and want xyz to be first declared as integer. I also know how many elements are there in the array so that is not an issue.

One option is to do as shown below, but this is a very tedious and can take lot of time

Dim xyz(0 to 19) as Integer
xyz(0)=1
xyz(2)=3
xyz(3)=5
...

Is there a shorter way to achieve the objective.

Thanks


Solution

  • Use two arrays, but why? Edit: see also comment of Raymond Wu

     Sub tst()
        Dim xyz() As Integer, zyx As Variant, i As Long
            zyx = Array(1, 3, 5, 10, 20, 30, 32, 45, 76, 89, 100, 34, 12, 0, 1, 100, 34, 45, 56, 67)
            ReDim xyz(LBound(zyx) To UBound(zyx))
                For i = LBound(xyz) To UBound(xyz)
                    xyz(i) = zyx(i)
                Next
        End Sub