[英]How do I create a link in Excel, with a formula in it, to another Workbook?
I'm not sure if this is even possible in Excel but this is what I need to do: 我不确定在Excel中是否甚至可以做到这一点,但这是我需要做的:
I have a column with a list of hotels and then another column which needs to pull data from each individual hotel's excel file. 我有一列包含酒店列表的列,然后是另一列,需要从每个酒店的excel文件中提取数据。 For example, cell A2 will have the name "Paris" for the hotel and then cell B2 will have the link:
例如,单元格A2的酒店名称为“巴黎”,然后单元格B2的链接为:
='G:\Hotels\Paris\Paris - Monthly\[Paris_summary_2018.xlsm]Feb'!$CD$89
I have lots of hotels I need to do this for in different sheets. 我有很多酒店需要在不同的表中进行此操作。 So I need the link to dynamically refer to whatever hotel is in column A and the title of the sheet, for example could I do something like this?
因此,我需要该链接来动态引用A列中的任何酒店和工作表的标题,例如,我可以这样做吗?
=''G:\Hotels\A2\A2 - Monthly\[A2_summary_2018.xlsm]Feb'!$CD$89
where A2 has the string "Paris". 其中A2具有字符串“ Paris”。 Also is there a way to dynamically refer to "Feb" depending on what sheet I am in the month will be the title
也有一种方法可以动态地引用“ Feb”,具体取决于我当月的工作表是标题
I am also open to using VBA for this. 我也愿意为此使用VBA。
I am not sure if you can use HYPERLINK
. 我不确定是否可以使用
HYPERLINK
。
Like this: 像这样:
=HYPERLINK("G:\Hotels\"&A2&"\"&A2&" - Monthly\["&A2&"_summary_2018.xlsm]Feb!"&$CD$89)
As long as you don't mind using VBA, you can easily generate the links with something like this: 只要您不介意使用VBA,就可以轻松生成如下所示的链接:
Sub generate_hotel_links()
Dim r As Range, c As Range
Dim s As String
' This is the range which all the hotel-locations are in
Set r = ThisWorkbook.Worksheets("Sheet1").Range("A1:A10")
On Error Resume Next
For Each c In r
' Generate the formula based on the current cell we are in
s = "=" & Chr(39) & "G:\Hotels\" & CStr(c) & "\" & CStr(c) & " - Monthly\[" & CStr(c) & "_summary_2018.xlsm]Feb" & Chr(39) & "!$CD$89"
' ...and put it in the neighbouring cell
c.Offset(0, 1).Formula = s
Next c
On Error Goto 0
End Sub
On Error Resume Next
will make the macro continue no matter what error pops up - the ideal case would be some more robust error handling, or a check for if the workbook / sheet actually exists before attempting to write the formula, but I'll leave it to you to attempt to write this if you feel the need improve the macro. On Error Resume Next
无论弹出什么错误,宏都将继续执行-理想的情况是进行更健壮的错误处理,或者在尝试编写公式之前检查工作簿/工作表是否确实存在,但我将离开如果您认为需要改进宏,可以尝试编写此代码。
If you want to just use generic Excel formulas, I'd advice having a look at the question and answers I posted in the comments to your question. 如果您只想使用通用的Excel公式,建议您看一下我在问题注释中发布的问题和答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.