【问题标题】:VBA Variant Index out of rangeVBA 变体索引超出范围
【发布时间】:2018-07-16 11:56:35
【问题描述】:

我是 VBA 新手,我需要扩展现有工作表并保持其格式。需要完成 7 个长度可变(以行为单位)和 14 列宽度的部分。所以我想做的是以下几点:

  • 找到部分开始的行
  • 从每个部分中选择数据并将其保存到一个数组中(我认为这是一个长度为 7 的数组,每个条目包含一个包含数据的 2 维数组)
  • 选择我的新数据并使用该新数据扩展现有数组(在上一步中创建)
  • 用我新创建的数组覆盖当前工作表
  • 添加格式

我设法完成了第 1 步,目前正在第 2 步苦苦挣扎:我需要创建一个长度可变的数组,我可以在其中插入数据。

到目前为止我的代码:

' this should create the array with the 7 entries
' "myArray" contains the row-numbers where the sections start
Function GenerateSheetArray(sheet As Worksheet, myArray As Variant) As Variant

    Dim finalArray As Variant
    Dim myInt As Integer

    'here each entry should be filled
    For i = 0 To 6
        myInt = myArray(i)
        finalArray(i) = GenerateArrayPart(sheet, myInt)
    Next

    GenerateSheetArray = finalArray

End Function

'This should fill each entry with the data of corresponding section
Function GenerateArrayPart(sheet As Worksheet, headline As Integer) As Variant
    Dim leftIndex As Integer, rightIndex As Integer, rowcount As Integer
    Dim sheetArray() As Variant

    rowcount = 0
    leftIndex = 1
    rightIndex = 14
    i = headline + 1

    Do While sheet.Cells(i, 1) <> ""
        rowcount = rowcount + 1
        i = i + 1
    Loop

    If (rowcount > 0) Then
        For colIndex = leftIndex To rightIndex
            For rowIndex = 1 To rowcount
                Row = headline + rowIndex
                sheetArray(rowIndex - 1, colIndex - 1) = sheet.Cells(Row, colIndex)
            Next
        Next
    End If

    GenerateArrayPart = sheetArray
End Function

现在我的问题是,VBA 在这一行抛出错误:

'atm rowIndex and colIndex are 1, Row is 40
'I know that there is data in that cell
sheetArray(rowIndex - 1, colIndex - 1) = sheet.Cells(Row, colIndex)

VBA 说:

索引超出范围

在方法GenerateArrayPart 中。 这怎么可能发生?我认为variant 几乎可以是一切,也不需要使用边界?

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    数组中没有任何值。因此,数组只被声明而不被维度化。

    试试这个:

    Dim finalArray As Variant
    Redim finalArray(6)
    

    现在,数组里面有 7 个值。从 0 到 6。同样的错误发生在 Function GenerateArrayPart 中,数组 sheetArray。在那里,您需要将数组声明为多维数组。例如。 Redim sheetArray (N, M).

    要查看一些小的工作示例,请查看以下代码:

    Sub TestMe()
    
        Dim finalArr As Variant
        ReDim finalArr(6)
    
        Dim i As Long
        For i = LBound(finalArr) To UBound(finalArr)
            finalArr = GenerateArrPart(i)
        Next i
    
        For i = LBound(finalArr) To UBound(finalArr)
            Debug.Print i; finalArr(i, i)
        Next i
    
    End Sub
    
    Public Function GenerateArrPart(a As Long) As Variant
    
        Dim i As Long
        Dim arrReturn As Variant
        ReDim arrReturn(a + 1, a + 1)
    
        For i = LBound(arrReturn) To UBound(arrReturn)
            arrReturn(i, i) = a * i
        Next i
    
        GenerateArrPart = arrReturn
    
    End Function
    

    这是输出:

     0  0 
     1  6 
     2  12 
     3  18 
     4  24 
     5  30 
     6  36 
     7  42 
    

    【讨论】:

    • 您的意思是我每次添加新值以扩大范围时都应该使用“redim”?因为Redim sheetArray(N, M) 需要两个常量,并且在这种情况下我需要表现得像列表一样,所以我不能只在该函数的一开始就使用它一次
    • @Rüdiger - 每次扩展数组时,都必须重新调整,是的。如果您想保留旧值,请使用单词Preserve。喜欢Redim Preserve finalArray(100)
    • @Rüdiger - 如果你不喜欢每次都RedimCollection 对象任你使用。
    猜你喜欢
    • 1970-01-01
    • 2016-04-26
    • 1970-01-01
    • 2018-03-01
    • 1970-01-01
    • 2014-08-18
    • 1970-01-01
    • 2023-03-27
    相关资源
    最近更新 更多