# How to fill a string array in code



## balloon_tom (Jan 5, 2006)

Hopefully this is an easy one. I'm using Excel 2003.

I want to fill a string array with data from code, let's say the days of the week. First I'll dimension the string:

Dim a1(7) As String

Now, I can of course enter each item individually"
a1(0)="SU"
a1(1)="MO"
...and so on.

But, is there any way to do this in one line, for example (which doesn't work):

a1 = Array("SU", "MO", "TU", "WE", "TH", "FR", "SA")?

Actually, I'm going to copy the array to the worksheet, so I guess I need a 2 dimensional array, like:

Dim a1(7,1)

such that I can say:

Range("A1:A7") = a1.

Any thoughts?
Thanks,
Tom


----------



## Greg8537481665 (Jan 31, 2009)

Yes, Try this..

If you can use the .NEt framwork I would recommend
Dim arrString() as string
arrString = NEW String(){"a","b","c"}
..I think you have to do it when you instantiate it with the NEW keyword.

but if your true intention is for a day of the week, I would recommend using:
Dim weekday As System.DayOfWeek providing you may reference .Net framework.

If you must use straight VBA then try
Dim strArray()
strArray = Array("a", "b", "c")
Dim i As Integer
For i = 0 To UBound(strArray)
MsgBox strArray(i)
Next i


----------

