Specifying Default Hyperlink Text

by Allen Wyatt
(last updated October 11, 2014)

7

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.

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

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

Hiding Errors

If you find the green and red squiggly underlines that Word adds to your document distracting, you might want a quick way to ...

Discover More

Adjusting Values with Formulas

Paste Special is a great tool that allows you to modify the values in a range of cells in your worksheets. You may want, ...

Discover More

Determining the Month of the Year

Your macro code may need to determine the month of the year represented by a particular date. You can find the desired ...

Discover More

Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!

More ExcelTips (ribbon)

Pasting HTML without Hyperlinks

Excel allows you to copy information from the web and paste it into a worksheet. Problem is, the pasting could take some time ...

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 workbook ...

Discover More

Links to Hyperlinks

Hyperlinks in a worksheet can be helpful or essential, depending on the nature of your data. If you create a link to a ...

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}] 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 9?

2014-11-04 11:54:00

Bigger Don

@Dennis & @Kim

Here's the easy solution for making InStrRev available on the worksheet. All I did was to make the UDF, pass-through the arguments from the worksheet to the VBA function, assigned the result and voila! a worksheet function mimicking what was previously a VBA-only function.

Function udf_InStrRev(StringCheck As String, _
StringMatch As String, _
Optional Start As Long = -1, _
Optional Compare As VbCompareMethod) As String
udf_InStrRev = InStrRev(StringCheck, StringMatch, Start, Compare)
End Function


2014-11-03 12:44:36

Dennis Costello

There is a somewhat simpler formula, too:

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

As with the original, the idea here is to figure out how many "" characters there are in the string, and then replace the last one with a non-printing character - hence CHAR(1) - and the find that character in the original string. We're still counting the "" by comparing the length of the original string against one with all the ""s removed, and using that as the "which instance to substitute" value. And then finding the sentinel.

My version of the formula is a little simpler in that instead of using another SUBSTITUTE to replace the leading characters (the result of the LEFT function) with a null string, you can simply use the RIGHT function to select all the characters other than the ones to the left (hence, the length of the original string minus the position of the sentinel).

And I agree with the earlier poster - this would be soooo much easier if we could use InStrRev in a function.


2014-10-13 10:41:17

Kim Carlton

Never mind - I figured it out by looking at additional information on the net. Clever. You would think Excel would provide an easier non-macroish way by exposing the VBA InStrRev function to the spreadsheet interface. Would certainly make life a lot easier.
Cheers!


2014-10-13 10:01:34

Bigger Don

@Willy...I was trying to think of that last week and too busy to look it up. Thanks.

That will fix them after the fact, and en masse. But if we put it in the appropriate event procedure it will do it as each on is entered.

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Hyperlinks.Count > 0 Then
Application.EnableEvents = False

Target.Hyperlinks(1).TextToDisplay = _
Mid(Target.Hyperlinks(1).TextToDisplay, InStrRev(Target.Hyperlinks(1).TextToDisplay, "") + 1)
Application.EnableEvents = True
End If
End Sub


2014-10-13 08:46:33

Kim Carlton

I'm confused on what the CHAR(1) is doing for the function. I copied this function into my spreadsheet and it is coming up with a #VALUE! error. An ASCII chart look up reveals a CHAR(1) is equivalent to SOH or Start of Heading. Can someone tell me how that is applied here? The last time I used SOH was about 100 years ago with paper tape and then magnetic tape @ 1600 bpi. Ah - the good ol days...


2014-10-11 10:21:14

Willy Vanhaelen

That works fine if you have only a few hyperlinks but if you have a bunch of them, a macro will be handy.

The one in tip can be drastically shortened though. 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.

My 6 line macro does the job of this tip's 17 line one more efficiently of course:

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


2014-10-11 09:51:35

David George

Unless I'm missing something, there's a much easier way to do this.

Why doesn't George just type the text he wants to appear in the cell, right click, and select "Hyperlink"? The dialog box lets him select the file he wants to link.

I have hundreds of these on my budgeting spreadsheet, and these links point to files all over my system, across several different drives.


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.