我是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': 下标超出范围
谢谢!
答案 0 :(得分:0)
您正在递增快速导致您访问不存在的数组值的内容。
Dim intArray(0, 9) As Integer
这意味着您将拥有索引(0,0),(0,1)等。请参阅本地工具栏中的此图片:
但是你正在增加
中的第一个索引For intRowIndex = 0 To 9
intArray(intRowIndex, intColumnIndex)
因此,第二次循环,您尝试访问(1,0)不存在。