Fixing Hyperlinks that somehow got changed by a user

sspatriots

Well-known Member
Joined
Nov 22, 2011
Messages
572
Office Version
  1. 365
Platform
  1. Windows
Hi,

I've had some success with various versions of the code below to fix hyperlinks that somehow get changed by a user. Not sure if they are saving the file to their hard drive and then back to the server to cause this or not. Regardless, I need a way to get this back to the way it was and my code doesn't seem to work. Not sure if I have a missing forward slash or backslash or what or maybe I have them backwards.

Here is what I have tried:

VBA Code:
Sub FixPOHyperlinksHH()
  
    Dim wb As Workbook
    Dim ws As Worksheet
    Dim tb As ListObject
    Dim OldStr As String, NewStr As String
    Dim hyp As Hyperlink

    Set wb = ThisWorkbook
    Set ws = wb.ActiveSheet 'Sheets("2023")

'    assumes Table is the first one on the ActiveSheet
    Set tb = ActiveSheet.ListObjects(1)

    ws.Activate

    OldStr = "C:\Users\HHenderson\AppData\Roaming\Microsoft"

    NewStr = "G:\DEManufacturing"

        For Each hyp In tb.ListColumns("PO#").DataBodyRange.Hyperlinks
    
            If InStr(1, hyp.Address, "/") > 0 Then
                hyp.Address = Replace(hyp.Address, OldStr, NewStr)
            
            Else
                hyp.Address = NewStr & "/" & hyp.Address
            End If
    
        Next hyp

End Sub
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Well, I found the code below that seems much simpler and it worked for now....

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, "C:\Users\HHenderson\AppData\Roaming\Microsoft", "G:\DEManufacturing", Compare:=vbTextCompare)

Next

Next



End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,215,196
Messages
6,123,575
Members
449,108
Latest member
rache47

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