PDA

View Full Version : Filling a Two Dimension Array



buddy2000
03-13-2011, 11:26 AM
I need to fill a two dimension array the first with months of the year. The second with numbers. Here is what I have tryed. I keep getting a syntax error on the Monthinfo line.



Dim I As Integer
Dim Monthinfo(0 To 11, 0 To 3) As Variant
Monthinfo = Array("January", "Febaury", "March", "April", "May", "June", "July", "August", "Sepetember", "October", "Novemeber", "December"), ("28","29","30","31")



'Initialize counter
I = 0
'Loop to fill the array
For I = 0 To 11
Monthinfo(I) = Monthinfo(I)


Next

JimmyTheHand
03-13-2011, 12:05 PM
Buddy,
the array defined by
Dim Monthinfo(0 To 11, 0 To 3) As Variant is a 12 times 4 elements matrix. You need 48 values to fill it completely.
It appears that you want the 12 month names and those 4 numbers as values. In this case you need two separate arrays:
Dim MonthInfo(0 To 11)
Dim DayInfo(0 to 3)
Monthinfo = Array("January", "Febaury", "March", "April", "May", "June", "July", "August", "Sepetember", "October", "Novemeber", "December")
DayInfo = Array("28","29","30","31")
Jimmy

buddy2000
03-13-2011, 12:28 PM
No I need to be able to do it with a two dimension array. I have done it with two seperate arrays. You are suppose to be able to it with a two dimesional array.

I am new to VBA so try to be a little understanding.

JimmyTheHand
03-13-2011, 12:42 PM
No offense intended, and I have no problem with your being new. I just don't understand how you want to fill a 48 element matrix with 16 values.
Imagine the two dimensional array as a table with 12 columns and 4 rows. The first row (12 cells) is filled with the month names. How do you want to distribute the 4 numbers among the remaining 36 cells?

mdmackillop
03-13-2011, 02:37 PM
You can create 1 array from 2 as below. Other than this, I can't see what you are after.
Option Explicit
Sub test()
Dim I As Integer, J As Integer
Dim Mths, Nums
Dim Monthinfo(0 To 11, 0 To 4) As Variant

Mths = Array("January", "Febaury", "March", "April", "May", "June", "July", "August", _
"Sepetember", "October", "Novemeber", "December")
Nums = Array("28", "29", "30", "31")

'Loop to fill the array
For I = 0 To 11
Monthinfo(I, 0) = Mths(I)
For J = 1 To 4
Monthinfo(I, J) = Nums(J - 1)
Next
Next
Cells(1, 1).Resize(12, 5) = Monthinfo
End Sub

buddy2000
03-13-2011, 10:40 PM
Ok I understand now. I did not understand that what I was trying to do was a 48 matrix array. You see I really do not understand arrays that well. I understand a little more now. Thank you for your help.