1

我是 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':下标超出范围

谢谢!

4

1 回答 1

0

您正在增加事物,这会很快导致您访问不存在的数组值。

Dim intArray(0, 9) As Integer

这意味着您将拥有索引 (0,0)、(0,1) 等。从本地工具栏中查看此图片:在此处输入图像描述

但是你正在增加你的第一个索引

For intRowIndex = 0 To 9
   intArray(intRowIndex, intColumnIndex) 

因此,第二次通过循环,您尝试访问不存在的 (1,0)。

于 2013-09-14T21:34:16.607 回答