by Allen Wyatt
(last updated October 11, 2014)
George often inserts hyperlinks into his worksheets. These hyperlinks are to files of various types that reside on his computer system. When he inserts the hyperlinks, the default hyperlink text that Excel inserts is the full path and file name for the file. George would like this hyperlink text, by default, to consist of only the file name, without the full path.
There is one very easy way to accomplish the desired outcome: Simply move the workbook into the same folder with the files to which you want hyperlinks. When you use the Insert Hyperlink dialog box, the filename—without the path—is then used for the link.
If that isn't feasible, then using the Insert Hyperlink dialog box does result in a full path ending up in both the Address field and Text to Display field of the Insert Hyperlink dialog box. There is no way to change this default behavior; you can only edit the default proposed by Excel. This means that you can, on an individual hyperlink basis, edit the Text to Display field in the Insert Hyperlink dialog box so that it shows just the file name, as desired.
Of course, this involves a lot of editing—hence the impetus for George's query. There are a couple of ways around this. The first is to not rely on the Insert Hyperlink dialog box. Instead, construct a formula that inserts the hyperlink using the HYPERLINK function. Let's say, for example, that you have, in cell C2, a path name to the folder containing your files:
Further, you could have a list of filenames in the range D2:D75, such as this:
MyWorkbook.xlsx MyDocument.docx MyPDF.pdf
You could place a formula such as this in cell E2:
=HYPERLINK($C$2 & D2,D2)
Copy the formula down into the range E3:E75, and you'll have your hyperlinks as you desire.
If you already have a bunch of inserted links in the spreadsheet (for example, in column A), you could use a bit larger formula to extract and display just the filename:
You could then hide the column containing the full-path hyperlinks so you see only the shortened versions that you want.
If you have many hyperlinks that you want to affect, you can use a macro to remove the path from all the existing hyperlinks.
Sub FixHyperlinkDesc() Dim h As Hyperlink Dim sRaw As String Dim iPos As Integer For Each h In ActiveSheet.Hyperlinks sRaw = h.TextToDisplay iPos = Instr(1, sRaw, "\") While (iPos > 0) sRaw = Mid(sRaw, iPos + 1, Len(sRaw)) iPos = Instr(1, sRaw, "\") Wend If sRaw <> h.TextToDisplay Then h.TextToDisplay = sRaw End If Next h End Sub
The only thing the macro touches is the display text for each hyperlink and it deletes everything before the final backslash. The macro won't affect anything that may appear as the result of a HYPERLINK function.
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (13279) applies to Microsoft Excel 2007, 2010, and 2013.
Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!
When creating an e-mail address hyperlink using the Insert Hyperlink dialog box, Excel allows you to enter a subject for ...Discover More
When you add a hyperlink to a worksheet, it consists of a minimum of two parts: display text and URL address. If you have ...Discover More
When you create a worksheet that is destined for viewing on the Web, you will want to specify the monitor resolution you ...Discover More
Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.