Changing lots of Excel hyperlinks at once - nearly got it working

henryJT

New Member
Joined
Oct 24, 2011
Messages
7
I need to change a lot of hyperlinks in a spreadsheet, as users where saving to their shared drive instead of a central location.
I have this code, which is nearly working...

Public Sub ChangeHyperlinks()
Dim ws As Worksheet
Dim hlink As Hyperlink
Dim p As Long
For Each ws In ActiveWorkbook.Worksheets
For Each hlink In ws.Hyperlinks
p = InStrRev(hlink.Address, "\\fileserver\profiles\LMM\Desktop")
If p > 0 Then
hlink.Address = "\\fileserver\documents\link files" & Mid(hlink.Address, p + 1)
End If
Next
Next
End Sub

However, what is happening when I run it, it's putting the new path in, then adding the old path afterwards, I want the old path removed and just the filename adding to the new. Is that easy to do?
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
In your case, the result from InStrRev was no different than InStr. Both would give the value of 1 if that path was in the hyperlink. That might have been confusing you with what you were expecting for the value of p. p isn't the count where the old hyperlink ended, it is where it started. InStrRev finds the beginning location of where the search string occurs searching right to left. Since the hyperlink path only occurs once, when found, it will be at the beginning of the hyperlink address; thus p = 1.

Try this replacement:

Code:
Public Sub ChangeHyperlinks()
    Dim ws As Worksheet
    Dim hlink As Hyperlink
    Dim p As Long
    For Each ws In ActiveWorkbook.Worksheets
        For Each hlink In ws.Hyperlinks
            p = InStrRev(hlink.Address, "\\fileserver\profiles\LMM\Desktop")
            If p > 0 Then
                hlink.Address = "\\fileserver\documents\link files" & Mid(hlink.Address, Len("\\fileserver\profiles\LMM\Desktop") + 1)
            End If
        Next
    Next
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top