constant array

  • Thread starter Thread starter Sam
  • Start date Start date
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
 
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
 
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
 
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Weird Behaviour of Compiler Directives? 14
Constants 2
arrays from Excel VBA 2
Spaces in folder name and file name 3
Cannot find path! 2
Const? 3
A constant array? 3
How to reference a constant? 4

Back
Top