How can I “ReDim Preserve” a 2D Array in Excel 2007 VBA so that I can add rows, not columns, to the array?

前端 未结 8 1496
梦如初夏
梦如初夏 2020-12-06 02:28

I\'m working with a dynamic array in Excel VBA. The number of columns (m) is fixed, however, I do not know how many rows (n) will be required.

The help documents st

8条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-06 03:03

    Solved my own question; here's how I got around my problem. I created a temporary array, copied the contents of myArray to the temporary Array, resized myArray, then copied the contents back from the temp array to myArray.

    tempArray = myArray
    ReDim myArray(1 To (UBound(myArray()) * 2), 1 To m)
    For i = 1 To n
         For j = 1 To m
              myArray(i, j) = tempArray(i, j)
         Next j
    Next i
    

    If anyone can suggest a more efficient way to do this, I'd love to hear it.

提交回复
热议问题