超链接.跟随错误:运行时错误"9":下标超出范围 [英] Hyperlinks.Follow error: Run-time error '9': Subscript out of range
问题描述
激活超链接后,下标超出了工作表选择行的范围
After I activate the hyperlink I get the subscript out of range at the sheet selection line
'Hyperlink aktivieren und Sheet Overview Results
Selection.Hyperlinks(1).Follow NewWindow:=True, AddHistory:=True
Worksheets("Overview Results").Select
AuswerteWb = ActiveWorkbook.Name
'ActiveWindow.Close
问题是,我有一个宏,该宏应使用文件的路径作为超链接,并从超链接的文件中选择工作表概述结果".
The thing is that I have a macro which should use a path of a file as hyperlink and select the sheet "overview Results" from the hyperlinked file.
但是我得到
运行时错误'9':下标超出范围
Run-time error '9': Subscript out of range
推荐答案
为什么使用 Hyperlinks.Follow
,而不是 Workbooks.Open
?如果要使用超链接打开新工作簿,则需要执行以下操作:
Why use Hyperlinks.Follow
and not Workbooks.Open
? If you are opening a new workbook using the hyperlink you will want to do something like this:
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
等.
I highly encourage you to learn about qualifying your statements (for example, Worksheets("")
is an unqualified statement) since this will cause you many headaches. Similarly, avoid Selection
, Select
, Activate
, ActiveWorkbook
, etc.
这篇关于超链接.跟随错误:运行时错误"9":下标超出范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!