运行时错误' 9'通过表格的代号,下标超出范围

时间:2017-08-17 19:12:29

标签: excel vba excel-vba

我遇到了超出范围错误的下标,我不知道这意味着什么。该错误特别发生在名为RedBorder的子区域中,该子区域在名为validateDropDown的子区域内调用,validateDropDown位于名为validationTools的模块中,因此是RedBorder。在ValidateSheet中调用validateDropDown,并在Workbook_Open子中从ThisWorkBook调用ValidateSheet。 之前我曾问过这个问题,但很多人都让我要创建一个最小,完整且可验证的例子。我对stackoverflow和vba都很新。提前致谢

像这样:

sheet8.ValidateSheet

这是我的ValidateSheet

Public Sub ValidateSheet()

   allowEdit

   ValidationTools.validateDropDown Sheet8, cbApplication, CB_APPLICATION_ERR_RANGE
   disallowEdit

End Sub

这是RedBorder:

Public Sub RedBorder(sheet As Worksheet, rangeString As String)

  With Worksheets("sheet").Range(rangeString).Borders
    .LineStyle = xlContinuous
    .Color = vbRed
    .Weight = xlThin
  End With

End Sub

这是validateDropDown:

Public Sub validateDropDown(sheet As Worksheet, ddl As msforms.ComboBox, rangeString As String)
  With sheet
   If ddl.ListIndex = -1 Then
    RedBorder sheet, rangeString
    'add message
    .Range(rangeString).Value = "Please make a selection"

   Else
    NoBorder sheet, rangeString
    'remove message
     .Range(rangeString).Value = ""
  End If
 End With
End Sub

1 个答案:

答案 0 :(得分:4)

sheet已经是Worksheet个对象,因此您不要将其放在引号中或使用Worksheets()部分。

Public Sub RedBorder(sheet As Worksheet, rangeString As String)

  With sheet.Range(rangeString).Borders
    .LineStyle = xlContinuous
    .Color = vbRed
    .Weight = xlThin
  End With

End Sub