constant array

S

Sam

Is there a way to defind an array of constants in VBA? I
tried the following and it would not compile.

Const a(1) As Double = 1
Const a(2) As Double = 2

Thanks,
Sam
 
T

Trevor Shuttleworth

Sam

one way:

Sub LoadArray()
Dim aA(1 To 5) As Double
Dim i As Long
For i = 1 To UBound(aA)
aA(i) = i
Next 'i
'For i = 1 To UBound(aA)
' Debug.Print i & " " & aA(i)
'Next 'i
End Sub

Regards

Trevor
 
S

Sam

Trevor,
Thanks for the suggestion. I want to avoid a call to a
procedure to do this. My specific problem is this: in
calculating the positions of the planets, I have to
evaluate a series of terms in the form A*Cos(B+C*t), where
A, B, and C are constants. This does not look too bad to
code in-line. However, when I have to sum anywhere from 32
to 64 of these, I could save myself a lot of problems if
the constants are in a array. My goal is to load the
constants into the array at compile time and not at run time.

Regards,
Sam
 
B

Bob Kilmer

The direct answer to your question is No.

You can do this, which is sort of close in a way, kind of but not really.

Sub Main()
Const s As String = "1,2,3,4,5"
Dim v As Variant, i As Integer
v = Split(s, ",")
For i = LBound(v) To UBound(v)
Debug.Print v(i)
Next i
End Sub
 
Top