【问题标题】:Hyperlinks.Follow error: Run-time error '9': Subscript out of rangeHyperlinks.Follow 错误:运行时错误“9”:下标超出范围
【发布时间】:2017-06-20 12:05:37
【问题描述】:

激活超链接后,下标在工作表选择行超出范围

'Hyperlink aktivieren und Sheet Overview Results
Selection.Hyperlinks(1).Follow NewWindow:=True, AddHistory:=True
Worksheets("Overview Results").Select
AuswerteWb = ActiveWorkbook.Name
'ActiveWindow.Close

问题是我有一个宏,它应该使用文件路径作为超链接,并从超链接文件中选择工作表“概述结果”。

但我明白了

运行时错误“9”:下标超出范围

【问题讨论】:

  • 你设置Option Base 1了吗?
  • 当您引用不存在的工作表名称时,可能会发生此错误。仔细检查工作表名称并检查前导和尾随空格。

标签: vba excel


【解决方案1】:

为什么使用Hyperlinks.Follow 而不是Workbooks.Open?如果您使用超链接打开一个新工作簿,您需要执行以下操作:

Dim OpenedFile as Workbook

' Skip any errors that would occur with a null link
On Error Resume Next
Set OpenedFile = Workbooks.Open(Selection.Value)
On Error GoTo 0

' Ensure that the file is set before operating on it
If Not OpenedFile Is Nothing Then
    Dim TargetWorksheet as Worksheet

    On Error Resume Next
    Set TargetWorksheet = OpenedFile.Worksheets("Overview Results")
    On Error GoTo 0

    ' We use the same Nothing check before operating on the worksheet
    If Not TargetWorksheet Is Nothing Then
        TargetWorksheet.Activate
    End If
End If

AuswerteWb = OpenedFile.Name
'ActiveWindow.Close

我强烈建议您了解如何限定您的语句(例如,Worksheets("") 是一个不合格的语句),因为这会让您很头疼。同样,避免SelectionSelectActivateActiveWorkbook等。

【讨论】:

  • 请不要忘记将答案标记为正确答案:)。我很高兴它对你有用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-12-15
  • 1970-01-01
  • 1970-01-01
  • 2013-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多