Specifying Default Hyperlink Text

Written by Allen Wyatt (last updated December 11, 2023)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365


4

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:

C:\Users\allen\Desktop\

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:

=HYPERLINK(A1,SUBSTITUTE(A1,LEFT(A1,FIND(CHAR(1),SUBSTITUTE(A1,
"\",CHAR(1),LEN(A1)-LEN(SUBSTITUTE(A1,"\",""))))),""))

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.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (13279) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Displaying Negative Percentages in Red

Excel includes quite a few different formats you can use for the information in a worksheet. One format that isn't as ...

Discover More

Understanding Default DATE Field Formatting

The DATE field is one of the most commonly used fields for placing dynamic information in your document. It is helpful to ...

Discover More

Creating a List of Files in a Directory

Do you need a list of all the files in a directory? It's easy to create if you use the proper command-line commands.

Discover More

Program Successfully in Excel! John Walkenbach's name is synonymous with excellence in deciphering complex technical topics. With this comprehensive guide, "Mr. Spreadsheet" shows how to maximize your Excel experience using professional spreadsheet application development tips from his own personal bookshelf. Check out Excel 2013 Power Programming with VBA today!

More ExcelTips (ribbon)

Converting to Hyperlinks in a Shared Workbook

When you enter a URL or e-mail address in a worksheet, Excel usually converts it to a clickable hyperlink. This doesn't ...

Discover More

Extracting URLs from Hyperlinks

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

Using Drag-and-Drop to Create a Hyperlink

If you open workbooks in two instances of Excel, you can use drag-and-drop techniques to create hyperlinks from one ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is four less than 6?

2023-12-11 08:09:30

Brian

Perhaps I'm missing the point here, but the Insert Hyperlink dialogue box has a "Text to display" box at the top. This can be any text string, so given that you must know what file you want to link to, why not just type it's name in the box? It can also be a formula for example =D2 & " " & D3 & " " & D4 and so (probably) most of the excel sheet functions.


2020-12-26 10:52:03

J. Woolley

Be aware: If you add the =HYPERLINK(...) formula to a cell that already has a standard Ctrl+K hyperlink, the original hyperlink remains precedent. The new HYPERLINK(...) formula replaces the text in the cell but does not override the original hyperlink. You can read about more issues re. the HYPERLINK function in this PDF: https://drive.google.com/file/d/1vAXFMwX43N7b-6fEhdY5E0PZRmIQ9zlA/view
which is part of My Excel Toolbox. See https://sites.google.com/view/MyExcelToolbox/


2020-12-25 09:15:44

Willy Vanhaelen

Oops: there is a typo in my previous post.
Replace the empty string "" with "\":
h.TextToDisplay=Mid(h.TextToDisplay,InStrRev(h.TextToDisplay,"\")+1


2020-12-24 13:01:04

Willy Vanhaelen

The macro in tip can be simplified a lot. First of all the While Wend loop is obsolete and maintained only for backwards compatibility. It
is now recommended to use the Do ... Loop statement that provides a more structured and flexible way to perform a looping.

But most of all it isn't needed here at all. It searches for the last instance of the backslash but VBA provides a function that does simply that in a single operation: InStrRev.

Here is my more concise version:

Sub FixHyperlinkDesc()
Dim h As Hyperlink
For Each h In ActiveSheet.Hyperlinks
h.TextToDisplay=Mid(h.TextToDisplay,InStrRev(h.TextToDisplay,"")+1)
Next h
End Sub


This Site

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.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.