Find and replace portions of hyperlink path

LackLuster63

New Member
Joined
Mar 14, 2022
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hello - I need to move a folder containing test reports to a new server location. There is a spreadsheet with hyperlinks to the test reports that will be broken when the folder is moved. I'd like to swap out the old folder location with a new one for each of the links in the spreadsheet. For example:

Old file path
EVrD23mkqjtNoAgekM2CVJ4BeVZFYbjp0h5TdENOLOY35A?e=vbqYwT

New file path
EVrD23mkqjtNoAgekM2CVJ4BeVZFYbjp0h5TdENOLOY35A?e=vbqYwT

An older post featured a similar question with the following code as a solution, but I wasn't able to make it work for my application:

Find and replace portions of hyperlinks​


Sub AlterHyperlinks()
Dim c As Range
Dim pos As Long
Dim tmp As String

Const sNewPath As String = "\\DAVINCI-1\COMMON\MSDS Database"

For Each c In Range("A2", Range("A" & Rows.Count).End(xlUp))
If c.Hyperlinks.Count > 0 Then
tmp = c.Hyperlinks(1).Address
pos = InStrRev(tmp, "\", InStrRev(tmp, "\") - 1)
If pos > 0 Then c.Hyperlinks(1).Address = sNewPath & Mid(tmp, pos)
End If
Next c
End Sub
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
As I see it, you want to change the "/Loc/taiwan/" part of each hyperlink to "/RandD/".
VBA Code:
Public Sub Change_Hyperlinks()

    Dim ws As Worksheet
    Dim hlink As Hyperlink
    
    For Each ws In ActiveWorkbook.Worksheets
        For Each hlink In ws.Hyperlinks
            hlink.Address = Replace(hlink.Address, "/Loc/taiwan/", "/RandD/", Compare:=vbTextCompare)
        Next
    Next
        
End Sub
 
Upvote 0
Solution
Thank you - that works. I think I had this over-complicated in my mind. I appreciate your help!
 
Upvote 0

Forum statistics

Threads
1,214,940
Messages
6,122,361
Members
449,080
Latest member
Armadillos

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