0

我目前正在开发一小块 vba 软件,它正在做一些 excel 工作表操作。我目前在下面代码中的循环的第一次迭代期间遇到了 indexOutOfRangeException。我已经在调试中运行了它,可以看到数组在请求的索引处有一个值,但我不断收到这个异常。

 Private Sub AddReportQuestions(ByRef ReportName As String, ByRef i As Integer, 
                               ByRef domainStart As Integer, ByRef domainEnd As Integer, 
                               ByRef xlReportWorkSheet As Excel.Worksheet,
                               ByRef xlQuestionWorkSheet As Excel.Worksheet)
    Dim row As Integer
    row = 0

    Dim maxPrintableRow = ((domainEnd - domainStart) * 4) + 7
    Dim maxQuestions = (domainEnd - domainStart) + 1

    Dim reportRange = xlReportWorkSheet.Range(xlReportWorkSheet.Cells(7, (i * 10) + 1), xlReportWorkSheet.Cells(maxPrintableRow, (i * 10) + 10))
    Dim questionRange = xlQuestionWorkSheet.Range("C" & domainStart & ":I" & domainEnd)

    Dim questionArray(maxQuestions, 7) As Object
    Dim reportArray(maxPrintableRow + 1, 10) As Object

    questionArray = questionRange.Value

    For j = 0 To maxQuestions
        ' Question
        reportArray(row, 0) = questionArray(j, 0)

        ' Resize Row Height
        xlReportWorkSheet.Rows(row + 7).RowHeight = 45

        row += 1

        ' Response and Comment
        reportArray(row, 0) = questionArray(j, 1)
        reportArray(row, 2) = questionArray(j, 4)

        ' Resize Row Height
        xlReportWorkSheet.Rows(row + 7).RowHeight = 45

        row += 1

        ' Likelihood and Consequence
        reportArray(row, 0) = "Likelihood:"
        reportArray(row, 2) = questionArray(j, 2)
        reportArray(row, 4) = "Consequence:"
        reportArray(row, 6) = questionArray(j, 3)

        row += 1

        ' Divider line
        reportArray(row, 0) = "________________________________________________________________________________________________________________________"

        row += 1
    Next

    reportRange.Value = reportArray
End Sub

questionArray 是这段代码中的违规者。

4

1 回答 1

0

您正在声明questionArrayreportArray作为对象,但是您将它们视为变量。将您的声明语句更改为

Dim questionArray as variant
Dim reportArray(maxPrintableRow + 1, 10) As String
于 2015-02-18T15:17:56.517 回答