ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   constant array (https://www.excelbanter.com/excel-programming/296970-constant-array.html)

Sam

constant array
 
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

Trevor Shuttleworth

constant array
 
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


"Sam" wrote in message
...
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




Tom Ogilvy

constant array
 
As I recall, you can't use the Const Keyword with an array.

--
Regards,
Tom Ogilvy

"Sam" wrote in message
...
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

constant array
 
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
-----Original Message-----
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


"Sam" wrote in message
...
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



.


Bob Kilmer[_2_]

constant array
 
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

"Sam" wrote in message
...
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





All times are GMT +1. The time now is 01:28 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com