简体   繁体   English

如何在Excel中创建一个带有公式的链接到另一个工作簿?

[英]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.

相关问题 Excel:如何始终链接到同一目录中的外部工作簿? - Excel: How do I link to an external workbook always in the same directory? 您如何使用Excel公式对颜色格式引用另一个工作簿中的单元格? - How do you reference cells in another workbook using an excel formula for Color Formatting? 如何在 Excel 中对另一个工作簿进行相对引用? - How do I make a relative reference to another workbook in Excel? 如何在excel中创建一个公式来搜索行中的值,然后从该列中的另一个单元格中获取值? - How do I create a formula in excel that searches for a value in row, and then takes the value from another cell in that column? 如何在客户端计算机上创建多标签Excel工作簿? - How do I create a multiple tab Excel workbook on a client machine? 如何在Excel工作簿中从DDE feed创建图形 - How do I create a graph from a DDE feed in excel workbook Excel COUNTIF带有链接到另一个工作簿 - Excel COUNTIF with link to another workbook Excel:如何创建一个公式来对动态范围的单元格求和? - Excel: How do I create a formula to sum a dynamic range of cells? 如何在Excel中创建替换功能只运行在另一个Excel工作簿中第一次出现的单词? - How do I make a Replace function in Excel only run through the first occurrence of a word in another Excel workbook? 如何使用VBA将内容从Excel工作簿中的目录链接到另一个Excel工作簿? - How to Link Contents from Contents in an Excel workbook to another Excel workbook using VBA?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM