简体   繁体   English

在另一个Workbook / CSV VBA中创建指向单元格的超链接

[英]Create Hyperlink to Cell in Another Workbook/CSV VBA

I'm currently working on a script in WorkbookA that notifies of a change in another Workbook (WorkbookB). 我目前正在WorkbookA中的脚本上工作,该脚本通知另一个Workbook(WorkbookB)中的更改。 I would like to add the functionality of being taken to that Workbook if the user would like to see the change. 如果用户希望看到更改,我想将被带到该工作簿的功能。 Currently, I'm running the code: 目前,我正在运行代码:

SelRangeA(iRow, 2) = "=HYPERLINK(""[C:\..\WorkbookB.csv]Sheet1!B4"",""CLICK HERE"")"

Which displays the proper Hyperlink in the spreadsheet : Click Here with contents: 在电子表格中显示正确的超链接: Click Here ,显示内容:

=HYPERLINK("[C:\..\WorkbookB.csv]Sheet1!B4","CLICK HERE")

However when I follow the link, it opens the requested Workbook with an error: 但是,当我点击链接时,它打开请求的工作簿并显示错误:

Reference is not valid. 引用无效。

Any insight on how to properly reference the required cell? 关于如何正确引用所需单元格的任何见解? Thanks! 谢谢!

A CSV file won't contain a "Sheet1". CSV文件将不包含“ Sheet1”。 The "sheet" name is derived from the name of the file. “工作表”名称是从文件的名称派生的。

So your code needs to be: 因此,您的代码必须是:

=HYPERLINK("[C:\..\WorkbookB.csv]'" & filename & "'!B4","CLICK HERE")

where filename has been set to the base part of the CSV file's filename (ie it needs to equate to "WorkbookB" in your example). 其中filename已设置为CSV文件文件名的基本部分(即,在您的示例中,该文件名必须等于“ WorkbookB”)。

If the link is in another workbook and you want Excel to open it if it isn't already open, you need to specify both the address and the sub-address (kind of like an anchor in an HTML link): 如果该链接位于另一个工作簿中,并且您希望Excel在尚未打开的情况下将其打开,则需要同时指定地址和子地址(类似于HTML链接中的锚点):

SelRangeA(iRow, 2) = "=HYPERLINK(""C:\Foo.xlsx#[C:\Foo.xlsx]Sheet1!A1"")"

EDIT: Note that for a .csv file, the worksheet name will default to the file name on open: 编辑:请注意,对于.csv文件,工作表名称将默认为打开时的文件名:

SelRangeA(iRow, 2) = "=HYPERLINK(""C:\Foo.csv#[C:\Foo.csv]Foo!A1"")"
                                                         '^^^   

One way to do this is to define a name (Formulas->Name Manager) whenever certain cells get changed. 一种实现方法是每当某些单元格更改时定义一个名称(Formulas-> Name Manager)。 For example if the name was defined as "changedcell" you would set the hyperlink as follows: 例如,如果名称定义为“ changedcell”,则可以按以下方式设置超链接:

=HYPERLINK("[C:..\\WorkbookB.csv#changedcell]","CLICK HERE") = HYPERLINK(“ [C:.. \\ WorkbookB.csv#changedcell]”,“点击此处”)

Just make sure the scope of the name is set to full workbook if there are multiple worksheets. 如果有多个工作表,只需确保名称的范围设置为完整的工作簿即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM