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

前端 未结 8 1503
梦如初夏
梦如初夏 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:04

    If you are developer - what is the difference between rows and columns? Using array(N, 2) (if you have 2 columns) is the same as array(2, N) - for which you can

    ReDim Preserve arr(1 to 2, 1 to N+1). 
    

    And the difference for you (as developer) will be to put the variable from the cycle in second place, instead of the first one:

    N = ubound(arr)
    FOR i=1 to N
        GetColumn1Value = arr(1, i)
        GetColumn2Value = arr(2, i)
    NEXT i
    

    Or you want this:

    N = ubound(arr)
    FOR i=1 to N
        GetColumn1Value = arr(i, 1)
        GetColumn2Value = arr(i, 2)
    NEXT i
    

    What is the difference?

提交回复
热议问题