redimension multidimensional arrays in Excel VBA

后端 未结 4 1157
难免孤独
难免孤独 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:26

    There are two issues in your code:

    1. When you Dim an array without specifying the lower bound it will by default be 0 based (unless you have specified Option Base 1). When explicitly specified, lower bound can be any number, not just 0 or 1
    2. For a multi dimensioned array, Redim Preserve can only change the last dimension, and then only the upper bound.

    In general, I find it better to always specify Lower and Upper bounds, eg

    Redim MyArray(1 to 10, 0 to 99)
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2021-01-28 03:37

    In case anyone has this problem here is how I solved it:

    <code>
    Function find_sentences_complex(instring As String) As Variant
    
    Dim xorr As String: xorr = ChrW(&H22BB)
    Dim triple_bar As String: triple_bar = ChrW(&H2261)
    Dim idisj As String: idisj = ChrW(&H2228)
    Dim cond As String: cond = ChrW(&H2192)
     Dim x As Integer, y As Integer, z As Integer, b As Integer
    Dim total As Integer, paren_closure As Integer, marker As Boolean
     Dim n As Integer
    
    Dim sent1() As Variant, sent3() As Variant
    
    'Dim final1d As Integer, final2d As Integer
    Dim b_arr() As Integer
    Dim b_max As Integer
    
    
    
    
       Dim temp_string As String
    
            For x = InStr(instring, "(") To Len(instring) Step 1
    
               temp_string = Mid(instring, x, 1)
    
                If Mid(instring, x, 1) = "(" Then
    
                    If marker = False Then
                    z = x
                    marker = True
                    End If
    
                total = total + 1
                ElseIf Mid(instring, x, 1) = ")" Then
                total = total - 1
    
    
                    If total = 0 Then
                    marker = False
                    b = b + 1
                    paren_closure = x
                    ReDim Preserve sent1(b)
                    sent1(b) = Mid(instring, z, (x - z) + 1)
                    End If
                End If
            Next
    
     Dim temp_sent1 As String
    total = 0
    marker = False
    
    
    
    b = 0
    
    
    Dim sent2()
    ReDim sent2(UBound(sent1), 5)
    
     For n = 1 To UBound(sent1)
     temp_sent1 = sent1(n)
     temp_sent1 = Mid(temp_sent1, 2, Len(temp_sent1) - 2)
     b = 0
        For x = 1 To Len(temp_sent1)
    
    
    
               temp_string = Mid(instring, x, 1)
    
                If Mid(temp_sent1, x, 1) = "(" Then
    
                    If marker = False Then
                    z = x
                    marker = True
                    End If
    
                total = total + 1
                ElseIf Mid(temp_sent1, x, 1) = ")" Then
                total = total - 1
    
    
                    If total = 0 Then
                    marker = False
                    b = b + 1
                    paren_closure = x
                    'ReDim Preserve sent2(n, b)
    
                    sent2(n, b) = Mid(temp_sent1, z, (x - z) + 1)
    
                    End If
                End If
            Next
    
            'this part of the code redimensions the side of the array
    
            ReDim Preserve b_arr(n)
            b_arr(n) = b
    
    
    Next
    
    b_max = MaxValOfIntArray(b_arr)
    ReDim Preserve sent2(UBound(sent1), b_max)
    
    
    
    
    
    
    
    
    
    End Function
    
    Public Function MaxValOfIntArray(ByRef TheArray As Variant) As Integer
    'This function gives max value of int array without sorting an array
    Dim i As Integer
    Dim MaxIntegersIndex As Integer
    MaxIntegersIndex = 0
    
    For i = 1 To UBound(TheArray)
        If TheArray(i) > TheArray(MaxIntegersIndex) Then
            MaxIntegersIndex = i
        End If
    Next
    'index of max value is MaxValOfIntArray
    MaxValOfIntArray = TheArray(MaxIntegersIndex)
    End Function
    </code>
    
    0 讨论(0)
  • 2021-01-28 03:43

    The problem that you have is that you cannot change the rank (dimensions) of an array with a redim statement.

    dim sent() creates a 1-rank array, redim sent2(x, y) assumes a 2-rank array. Try dim sent(,).

    Also, it will improve performance (and code robustness) if you use

    dim sent1() as string
    dim sent2(,) as string
    
    0 讨论(0)
提交回复
热议问题