我真的是VBA的新手,我已经在这个问题上尝试了一整天,所以任何帮助和提示都很感谢。我在努力找出我做错了什么。
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim intArray(0, 9) As Integer
Dim strTarget As String
Dim blnFound As Boolean
Dim intRowIndex As Integer
Dim intColumnIndex As Integer
Dim intMatchIndex As Integer
For intRowIndex = 0 To 9
intArray(intRowIndex, intColumnIndex) = Cells(1, Chr(65 + intRowIndex))
Next intRowIndex
strTarget = "Q"
blnFound = False
For intRowIndex = 0 To 1
For intColumnIndex = 0 To 9
If strTarget = intArray(intRowIndex, intColumnIndex) Then
blnFound = True
Exit For
End If
Next intColumnIndex
If blnFound Then
Exit For
End If
Next intRowIndex
If blnFound Then
MsgBox "Match was found at index " & intMatchIndex
Else
MsgBox "No Match found"
End If
End Sub当我调试时,它在这一行停止:
intArray(intRowIndex, intColumnIndex) = Cells(1, Chr(65 + intRowIndex))出现错误:运行时错误'9':下标超出范围
谢谢!
发布于 2013-09-15 05:34:17
您正在递增的内容很快会导致您访问不存在的数组值。
Dim intArray(0, 9) As Integer这意味着你将有索引(0,0),(0,1),等等。从局部变量工具栏可以看到这张图片:

中的第一个索引递增。
For intRowIndex = 0 To 9
intArray(intRowIndex, intColumnIndex) 因此,在第二次循环中,您尝试访问并不存在的(1,0)。
https://stackoverflow.com/questions/18806390
复制相似问题