Array values variable

  • Thread starter Thread starter tcb
  • Start date Start date
T

tcb

Can I create a variable, populate it with values, and use that
variable in an array?

I know this below won't work:

Dim arr_PrimeArray1 As Variant
Dim ArrayValues As Variant

ArrayValues = "1, 2, 3, 5, 7"

arr_PrimeArray1 = Array(ArrayValues)

as it would recognize this as one value: "1, 2, 3, 5, 7"

and understand why, but is there another way?

I want it to recognize at as several individual values:

arr_PrimeArray1 = Array(1, 2, 3, 5, 7)
 
Sure can. Look up "array" in the VBA Help file; there's lots of useful
information (at least in A2002).

You can declare a fixed array or a dynamic array, of any datatype you want;
you can change the index base from the default of 0 to 1.

For example, the following will set the index base to 1, fill a
one-dimensional integer array with values of 1 to 10, and print 3 in the
immediate window:
Option Base 1
Public Sub ArrayTest()
Dim MyArray(10) As Integer
Dim i As Integer
For i = 1 To 10
MyArray(i) = i
Next i
Debug.Print MyArray(3)
End Sub

HTH,

Rob
 
Thanks, from your example I was able create the function I needed. Is
it also possible to dynamically create an array with text, like "Jan",
"Feb", etc?
 
Sure. Just declare it appropriately, and fill the values anyway you want.
Eg.
Dim MyStringArray(11) As String
MyStringArray(0) = "Jan"
MyStringArray(1) = "Feb"
...

Or loop through months:
For i = 0 to 11
MyStringArray(i) = MonthName(i + 1, True)
Next i

Rob
 
Thanks, from your example I was able create the function I needed. Is
it also possible to dynamically create an array with text, like "Jan",
"Feb", etc?

Take a look at the Split() function - e.g.
Split("Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec", ",")

John W. Vinson [MVP]
 
Back
Top