ReDim Preserve to a Multi-Dimensional Array in Visual Basic 6

前端 未结 10 391
孤城傲影
孤城傲影 2020-11-29 09:44

I\'m using VB6 and I need to do a ReDim Preserve to a Multi-Dimensional Array:

 Dim n, m As Integer
    n = 1
    m = 0
    Dim arrCity() As String
    ReDi         


        
相关标签:
10条回答
  • 2020-11-29 10:03
    Function Redim2d(ByRef Mtx As Variant, ByVal QtyColumnToAdd As Integer)
        ReDim Preserve Mtx(LBound(Mtx, 1) To UBound(Mtx, 1), LBound(Mtx, 2) To UBound(Mtx, 2) + QtyColumnToAdd)
    End Function
    
    'Main Code
    sub Main ()
        Call Redim2d(MtxR8Strat, 1)  'Add one column
    end sub
    
    'OR
    sub main2()
        QtyColumnToAdd = 1 'Add one column
        ReDim Preserve Mtx(LBound(Mtx, 1) To UBound(Mtx, 1), LBound(Mtx, 2) To UBound(Mtx, 2) + QtyColumnToAdd)
    end sub
    
    0 讨论(0)
  • 2020-11-29 10:06

    Easiest way to do this in VBA is to create a function that takes in an array, your new amount of rows, and new amount of columns.

    Run the below function to copy in all of the old data back to the array after it has been resized.

     function dynamic_preserve(array1, num_rows, num_cols)
    
            dim array2 as variant
    
            array2 = array1
    
            reDim array1(1 to num_rows, 1 to num_cols)
    
            for i = lbound(array2, 1) to ubound(array2, 2)
    
                   for j = lbound(array2,2) to ubound(array2,2)
    
                          array1(i,j) = array2(i,j)
    
                   next j
    
            next i
    
            dynamic_preserve = array1
    
    end function
    
    0 讨论(0)
  • 2020-11-29 10:08

    In regards to this:

    "in my task I have to change the whole array (2 dimensions"

    Just use a "jagged" array (ie an array of arrays of values). Then you can change the dimensions as you wish. You can have a 1-D array of variants, and the variants can contain arrays.

    A bit more work perhaps, but a solution.

    0 讨论(0)
  • 2020-11-29 10:12

    I haven't tested every single one of these answers but you don't need to use complicated functions to accomplish this. It's so much easier than that! My code below will work in any office VBA application (Word, Access, Excel, Outlook, etc.) and is very simple. Hope this helps:

    ''Dimension 2 Arrays
    Dim InnerArray(1 To 3) As Variant ''The inner is for storing each column value of the current row
    Dim OuterArray() As Variant ''The outer is for storing each row in
    Dim i As Byte
    
        i = 1
        Do While i <= 5
    
            ''Enlarging our outer array to store a/another row
            ReDim Preserve OuterArray(1 To i)
    
            ''Loading the current row column data in
            InnerArray(1) = "My First Column in Row " & i
            InnerArray(2) = "My Second Column in Row " & i
            InnerArray(3) = "My Third Column in Row " & i
    
            ''Loading the entire row into our array
            OuterArray(i) = InnerArray
    
            i = i + 1
        Loop
    
        ''Example print out of the array to the Intermediate Window
        Debug.Print OuterArray(1)(1)
        Debug.Print OuterArray(1)(2)
        Debug.Print OuterArray(2)(1)
        Debug.Print OuterArray(2)(2)
    
    0 讨论(0)
提交回复
热议问题