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

前端 未结 10 578
孤城傲影
孤城傲影 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 09:52

    Since VB6 is very similar to VBA, I think I might have a solution which does not require this much code to ReDim a 2-dimensional array - using Transpose, if you are working in Excel.

    The solution (Excel VBA):

    Dim n, m As Integer
    n = 2
    m = 1
    Dim arrCity() As Variant
    ReDim arrCity(1 To n, 1 To m)
    
    m = m + 1
    ReDim Preserve arrCity(1 To n, 1 To m)
    arrCity = Application.Transpose(arrCity)
    n = n + 1
    ReDim Preserve arrCity(1 To m, 1 To n)
    arrCity = Application.Transpose(arrCity)
    

    What is different from OP's question: the lower bound of arrCity array is not 0, but 1. This is in order to let Application.Transpose do it's job.

    Note that Transpose is a method of the Excel Application object (which in actuality is a shortcut to Application.WorksheetFunction.Transpose). And in VBA, one must take care when using Transpose as it has two significant limitations: If the array has more than 65536 elements, it will fail. If ANY element's length exceed 256 characters, it will fail. If neither of these is an issue, then Transpose will nicely convert the rank of an array form 1D to 2D or vice-versa.

    Unfortunately there is nothing like 'Transpose' build into VB6.

提交回复
热议问题