2017-06-20 127 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

請不要忘記標記答案爲正確的答案:)。我很高興它爲你工作。 –