我目前正在开发一小段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是此代码中的罪犯。
答案 0 :(得分:0)
您将questionArray
和reportArray
声明为对象,但是您将它们视为变量。将您的声明声明更改为
Dim questionArray as variant
Dim reportArray(maxPrintableRow + 1, 10) As String