简体   繁体   中英

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:

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. For example, cell A2 will have the name "Paris" for the hotel and then cell B2 will have the link:

  ='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?

=''G:\Hotels\A2\A2 - Monthly\[A2_summary_2018.xlsm]Feb'!$CD$89

where A2 has the string "Paris". Also is there a way to dynamically refer to "Feb" depending on what sheet I am in the month will be the title

I am also open to using VBA for this.

I am not sure if you can use 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:

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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