简体   繁体   中英

Excel - Hyperlink to file in same directory as xlsx file

I am trying to add a hyperlink to an Excel cell that leads to a file that is on the same directory as the xlsx file. I achieved that with this

=HYPERLINK("file.ext")

but the cell looks like this

file.ext (like link, blue and underlined)

and I want to type something else, eg

Click here (like link, blue and underlined)

I know I can do that with regular hyperlink from "Insert" tab but I want to do it with HYPERLINK function or some other function.

Perhaps something like:

=HYPERLINK("file:///C:\\TestFolder\\Book1.xls#Sheet2!B9","click here")

=HYPERLINK("file.ext","Click here")

或者

=HYPERLINK("file.ext";"Click here")

The answer above is not working with newer versions of Office. The solution below should work for old and new versions of office. I tested it with Office 2016.

Multiple cells

First we get the full directory of the current spread sheet:

B1 =CELL("filename")

The cell content for my example is "C:[Book1.xlsx]Sheet1". Note that also the file name and sheet is included. To remove the file name and sheet I used the following code:

C1 =LEFT(B1,FIND("|",SUBSTITUTE(B1,"\\","|",LEN(B1)-LEN(SUBSTITUTE(B1,"\\","")))))

The result is the file directory "C:\\". Of course this works also with other directories. In the next cell I entered the target file name:

D1 Test.txt

I finished by combining directory and filename into a hyperlink:

E1 =HYPERLINK(C1&D1)

One cell

It is also possible to combine everything into one cell:

B3 =HYPERLINK(LEFT(CELL("filename"),FIND("|",SUBSTITUTE(CELL("filename"),"\\","|",LEN(CELL("filename"))-LEN(SUBSTITUTE(CELL("filename"),"\\","")))))&"Test.txt")

Settings

Depending on your windows settings (list separator) you have to replace all "," with ";" in the code. Furthermore you have to replace "Test.txt" with your target file. The target file has to be in the same directory like the Excel file.

Source

https://social.msdn.microsoft.com/Forums/office/en-US/5d94e756-2202-4bbc-8795-cc6ba28e8a1b/excel-hyperlinkfile-format-for-a-relative-file?forum=exceldev

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