2017-06-20 209 views
1

我启动我得到的下标越界在纸张选择线的超级链接后Hyperlinks.Follow错误:运行时错误“9”:下标越界

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

的事情是,我有一个应该使用文件路径作为超链接的宏,并从超链接文件中选择工作表“overview results”。

,但我得到

Run-time error '9': Subscript out of range

+0

您是否设置了“Option Base 1”? –

+0

当您引用不存在的工作表名称时,可能会发生此错误。仔细检查表单名称并检查前导空格和尾随空格。 – braX

回答

2

为什么使用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("")是不合格的声明),因为这会导致你很多头痛。同样,请避免Selection,Select,Activate,ActiveWorkbook等。

+0

谢谢布兰登和佩赫。现在正在工作 –

+0

请不要忘记标记答案为正确的答案:)。我很高兴它为你工作。 –