My first multidimension array- Can I set up each line at once?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dk4300
    New Member
    • Mar 2007
    • 68

    My first multidimension array- Can I set up each line at once?

    I'm writing my first 2 dimensional array-
    I understand this for one dimension:
    Code:
    MyArray = Array("Name", "Date", "Error")
    and also using the split function,

    I like that you can do one line at a time, but for 2 dimension, I have only found this:
    Code:
    MyArray(0,1) = "Name"
    MyArray(0,2) = "Date"
    MyArray(0,3) = "Error"
    MyArray(1,0) = "Blah"
    MyArray(1,2) = "Blah2"
    MyArray(1,3) = "Blah3"
    which does each variable individually.
    I have a 9x7 table and having to do that 63 times doesn't seem like it can be right.
    How do I set up 1 array with 9 lines and 7 columns, one line at a time?
    Thanks
  • Mihail
    Contributor
    • Apr 2011
    • 759

    #2
    Code:
    Dim numRows As Integer
    Dim numCols As Integer
        numRows = 9
        numCols = 7
     
    Dim MyArray() As Variant, i As Integer, j As Integer
        ReDim MyArray(numRows, numCols)
        For i = 1 To numRows
            For j = 1 To numCols
                MyArray(i, j) = "Row " & i & ", Col " & j
                Debug.Print MyArray(i, j)
            Next j
        Next i
    and a useful link to learn more about arrays:

    Comment

    • NeoPa
      Recognized Expert Moderator MVP
      • Oct 2006
      • 32633

      #3
      What you've left out of your question is the declaration of MyArray, which must not be as an array, but as a variant that can take the form of an array when called upon to.

      Code:
      MyArray = Array("Name", "Date", "Error")
      The above code doesn't work with properly defined (or dimensioned) array variables (which is pretty unfortunate as most of the powerful functions that help with array work fall into the same category).

      I believe this to be a fundamental omission on the part of the designers, and the lack of reference to it in the Help system particularly confusing for beginners and the more experienced alike.

      I'm afraid the use of multi-dimensional arrays pretty well excludes the use of such helpful array-returning functions as Split() and Array().

      Comment

      Working...