Excel VBA中的redimension多维数组

时间:2014-08-25 01:18:26

标签: excel vba

看看以下代码。我的问题是我无法弄清楚如何重新定义n整数和b整数。我正在做的是数组sent1已经工作,它填充了大约4个句子。我需要仔细阅读每一句话,但我遇到了麻烦。

dim sent1() 
dim sent2()

dim n as integer, b as integer, x as integer
dim temp_sent as string
b = 0
For n = 1 to ubound(sent1)

temp_sent = sent1(n)
    for x = 1 to len(temp_sent1)
    code
    if a then
        b = b + 1
        '**THIS IS THE PART OF THE CODE THAT IS NOT WORKING**
        redim preserve sent2(1 to ubound(sent1), b)
        sent2(n,b) = [code]
    next
next

4 个答案:

答案 0 :(得分:1)

您的代码中存在两个问题:

  1. 当你Dim数组没有指定下限时,默认情况下它将基于0(除非你指定了Option Base 1)。明确指定时,下限可以是任何数字,而不仅仅是01
  2. 对于多维数组,Redim Preserve只能更改 last 维度,然后才能更改上限。
  3. 一般来说,我发现总是指定下限和上限更好,例如

    Redim MyArray(1 to 10, 0 to 99)
    

答案 1 :(得分:0)

您是否有任何特定原因要/必须使用数组?

如果没有,我建议改用集合。您也可以拥有嵌套集合,例如

Dim dimension1 As New Collection
Dim dimension2 AS New Collection

dimension1.Add dimension2

这样,您就不必担心手动增加尺寸了。如果你需要将它转换回2D数组,你可以在最后做到这一点

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

答案 2 :(得分:-1)

您遇到的问题是您无法使用redim语句更改数组的等级(维度)。

dim sent()创建一个1级数组,redim sent2(x, y)假设一个2级数组。试试dim sent(,)

此外,如果您使用

,它将提高性能(和代码健壮性)
dim sent1() as string
dim sent2(,) as string

答案 3 :(得分:-1)

如果有人遇到这个问题我就是这样解决的:

<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>