Can an array be declared as a constant?
I declared a String
constant of "1,2,3,4,5"
and then used Split
to create a new array, like so:
Public Const myArray = "1,2,3,4,5"Public Sub createArray() Dim i As Integer A = Split(myArray, ",") For i = LBound(A) To UBound(A) Debug.Print A(i) Next iEnd Sub
When I tried to use ReDim
or ReDim Preserve
on A
it did not let me. The downfall of this method is that you can still edit the values of the array, even if you can't change the size.
How about making it a function? Such as:
Public Function myConstant(ByVal idx As Integer) As Integer myConstant = Array(2, 13, 17, 23)(idx - 1)End FunctionSub Test() Debug.Print myConstant(1) Debug.Print myConstant(2) Debug.Print myConstant(3) Debug.Print myConstant(4)End Sub
Nobody can change it, resize it, or edit its content... Moreover, you can define your constants on just one line!