Trying to help out in a Forum in which a user asked to update all the hyperlinks within a workbook excluding 2 worksheets. This happens if your servers get replaced, or you migrate your data to a new server …
So your hyperlink were pointing to say ‘\\192.168.22.127\Docs\’ and you want to update them all to use ‘\\167.25.235.27\Engineering\Manuals\’ instead, or you move your documents from the ‘C:\’ drive to the ‘G:\’ drive, the following will enable you to do so with a single command.
Anyways, in case it can help anyone else, here was what I came up with.
'---------------------------------------------------------------------------------------
' Procedure : UpdateHyperlinks
' Author : Daniel Pineault, CARDA Consultants Inc.
' Website : http://www.cardaconsultants.com
' Purpose : Loop through all the worksheet within the current workbook and update the
' hyperlinks to a new path
' Copyright : The following is release as Attribution-ShareAlike 4.0 International
' (CC BY-SA 4.0) - https://creativecommons.org/licenses/by-sa/4.0/
' Req'd Refs: None required
' Req'd Procs: IsInArray()
'
' Input Variables:
' ~~~~~~~~~~~~~~~~
' sUpdateFrom : The path to replace
' sUpdateTo : The new path to use
' aSkipSheets : An array of sheets to exclude from the update process,
' omit to process all the worksheets
'
' Usage:
' ~~~~~~
'Call UpdateHyperlinks("\\192.168.22.127\Docs\", _
' "\\167.25.235.27\Engineering\Manuals\")
''Call UpdateHyperlinks("C:\", _
' "G:\"
' Array("Archive"))
'
' Revision History:
' Rev Date(yyyy/mm/dd) Description
' **************************************************************************************
' 1 2018-11-26 Initial Release (Forum Help)
'---------------------------------------------------------------------------------------
Public Sub UpdateHyperlinks(sUpdateFrom As String, _
sUpdateTo As String, _
Optional aSkipSheets As Variant)
Dim oWS As Excel.Worksheet
Dim oHlnk As Excel.Hyperlink
Dim bProcessSheet As Boolean
On Error GoTo Error_Handler
For Each oWS In Worksheets
bProcessSheet = False
If IsMissing(aSkipSheets) Then
'No exceptions
bProcessSheet = True
Else
'There are sheets to be skipped so let check if this is one of them
If IsInArray(oWS.Name, aSkipSheets) = False Then bProcessSheet = True
End If
If bProcessSheet = True Then
For Each oHlnk In oWS.Hyperlinks
'Update the actual hyperlink address
oHlnk.Address = Replace(oHlnk.Address, _
sUpdateFrom, _
sUpdateTo)
'Update the displayed text
oHlnk.TextToDisplay = Replace(oHlnk.TextToDisplay, _
sUpdateFrom, _
sUpdateTo)
Next oHlnk
End If
Next oWS
Error_Handler_Exit:
On Error Resume Next
If Not oHlnk Is Nothing Then Set oHlnk = Nothing
If Not oWS Is Nothing Then Set oWS = Nothing
Exit Sub
Error_Handler:
MsgBox "The following error has occurred" & vbCrLf & vbCrLf & _
"Error Number: " & Err.Number & vbCrLf & _
"Error Source: UpdateHyperlinks" & vbCrLf & _
"Error Description: " & Err.Description & _
Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _
, vbOKOnly + vbCritical, "An Error has Occurred!"
Resume Error_Handler_Exit
End Sub
'Source: https://stackoverflow.com/questions/10951687/how-to-search-for-string-in-an-array/10952705#10952705
Function IsInArray(stringToBeFound As String, arr As Variant) As Boolean
IsInArray = (UBound(Filter(arr, stringToBeFound)) > -1)
End Function