redimension multidimensional arrays in Excel VBA

后端 未结 4 1162
难免孤独
难免孤独 2021-01-28 02:59

Take a look at the following code. What my problem is is that I can\'t figure out how to redimension the n integer and the b integer. What I\'m doing is the array sent1 is alr

4条回答
  •  余生分开走
    2021-01-28 03:27

    Is there any specific reason why you want to / must use arrays?

    If not, I'd suggest using collections instead. You can also have nested collections, e.g.

    Dim dimension1 As New Collection
    Dim dimension2 AS New Collection
    
    dimension1.Add dimension2
    

    etc.

    That way, you won't have to worry about increasing dimensions manually at all. If you need to convert it back to a 2D Array, you can do sth like this in the end

    Dim item AS Variant
    Dim subCollection AS Collection
    
    Dim nRows AS Integer
    Dim nCols AS integer
    
    ' assuming "col" is your jagged collection
    
    nRows = col.Count
    For Each item in col
      If TypeOf item is Collection
        Set subCollection = item
        If subCollection.Count > nCols Then
          nCols = subCollection.Count
        End If
      Next item
    Next item
    
    Dim result(nRows, NCols) As Variant
    
    ' Now loop through the collections again and fill the result array
    

提交回复
热议问题