1

enter image description hereI'm trying to add an extension to all embedded hyperlinks on an Excel worksheet. I recorded a macro by doing one cell at a time but is not efficient. Can someone help me streamline the macro so that it knows to look at all hyperlinks, open, and insert additional information at the end tail of the existing hyperlink.

Sub Macro5()
'
' Macro5 Macro
' test
'
' Keyboard Shortcut: Ctrl+Shift+H
'
    Range("H1").Select
    ActiveCell.FormulaR1C1 = "?u=76208058&auth=true"
    Range("C2").Select
    Selection.Hyperlinks(1).Address = _
        "https://www.linkedin.com/learning/teaching-techniques-classroom-management?u=76208058&auth=true"
    Range("C3").Select
    Selection.Hyperlinks(1).Address = _
        "https://www.linkedin.com/learning/learning-how-to-increase-learner-engagement?u=76208058&auth=true"
    Range("C4").Select
    Selection.Hyperlinks(1).Address = _
        "https://www.linkedin.com/learning/teaching-with-technology?u=76208058&auth=true"
End Sub

1 Answer 1

1

Add String to Hyperlinks

  • The first code changes the hyperlink addresses of all cells in the specified worksheet, while the second changes only the hyperlink addresses in a specified column of the worksheet.
  • Adjust the values in the constants section appropriately.
  • The If statement checks if the current hyperlink has already been modified.

The Code

Option Explicit

' For the whole sheet:
Sub addTailSheet()

' Keyboard Shortcut: Ctrl+Shift+H

    Const SheetName As String = "Sheet1"
    Const TailCell As String = "H1"

    Dim ws As Worksheet
    Dim hyp As Hyperlink
    Dim Tail As String

    Set ws = ThisWorkbook.Worksheets(SheetName)

    With ws
        Tail = .Range(TailCell).Value
        For Each hyp In .Hyperlinks
            If Right(hyp.Address, Len(Tail)) <> Tail Then
                hyp.Address = hyp.Address & Tail
            End If
        Next
    End With

    MsgBox "Hyperlinks modified."

End Sub

' For a column:
Sub addTailColumn()

' Keyboard Shortcut: Ctrl+Shift+H

    Const SheetName As String = "Sheet1"
    Const TailCell As String = "H1"
    Const TailColumn As Variant = "C"  ' e.g. "C" or 3

    Dim ws As Worksheet
    Dim hyp As Hyperlink
    Dim Tail As String

    Set ws = ThisWorkbook.Worksheets(SheetName)

    With ws.Columns(TailColumn)
        Tail = .Parent.Range(TailCell).Value
        For Each hyp In .Hyperlinks
            If Right(hyp.Address, Len(Tail)) <> Tail Then
                hyp.Address = hyp.Address & Tail
            End If
        Next
    End With

    MsgBox "Hyperlinks modified."

End Sub
Sign up to request clarification or add additional context in comments.

3 Comments

Hi @VBasic2008, thank you for your support. I'm super new to VB macros and having a really hard time understanding what are constants, which values am I looking for? Also, when I run the macro the following line is highlighted as bright yellow - can you please explain why that is? Set ws = ThisWorkbook.Worksheets(SheetName) I appreciate your patience with someone as new as me. I updated my question and attached a screen grab of my Visual Basic UI as reference.
ok, I change one thing and it's working now Const SheetName As String = "ActiveSheet" However is there a way for me to identify to always grab "Hi" from a specific sheet so that I don't have to make sure "HI" in every sheet has the value?
@PamelaTellez: You have placed the code in a wrong place. In VBE you should right-click your VBAProject (Master Campus...) and select Insert > Module. Then when you select Modules under your VBAProject, double-click Module1 and copy/paste the code there. Then you have to change the "Sheet1" to the sheet name where you want to change the hyperlinks. The sheet names are the ones in parentheses (). If you want to change the hyperlinks on several sheets, then the code has to be modified. You can always edit your question via the edit button below your post to add more info.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.