繁体   English   中英

运行时错误9

[英]Run-time error 9

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

谢谢!

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

Dim intArray(0, 9) As Integer

这意味着您将拥有索引(0,0),(0,1)等。请从本地工具栏上看到以下图片: 在此处输入图片说明

但是您正在增加第一个索引

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM