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

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
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,214,982
Messages
6,122,575
Members
449,089
Latest member
Motoracer88

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