Run time Error 5 trying to add hyperlink

sharky12345

Well-known Member
Joined
Aug 5, 2010
Messages
3,404
Office Version
  1. 2016
Platform
  1. Windows
I'm using this procedure to try and add a hyperlink to a range of cells but I keep getting a run time error:

VBA Code:
    Dim LastRow As Long
    Dim WS As Worksheet
    Dim Cell As Range

    Set WS = ThisWorkbook.Sheets("COPY")
    LastRow = WS.Range("A65536").End(xlUp).Row
    
    Application.ScreenUpdating = False

    For Each Cell In WS.Range("A2:A" & LastRow)
    
        Path = "ds://" & Cell.Offset(0, 7).Value
        ScreenTip = "Link"
        TextToDisplay = Cell.Offset(0, 1).Value
        Cell.Offset(0, 8).Select
        ActiveCell.Hyperlinks.Add Anchor:=Selection, Address:=Path, ScreenTip:=ScreenTip, TextToDisplay:=TextToDisplay

    Next

    WS.Range("I2:I" & LastRow).Select
    With Selection.Font
        .Name = "Tahoma"
        .Size = 8
        .Strikethrough = False
        .Superscript = False
        .Subscript = False
        .OutlineFont = False
        .Shadow = False
        .Underline = xlUnderlineStyleSingle
        .ThemeColor = xlThemeColorHyperlink
        .TintAndShade = 0
        .ThemeFont = xlThemeFontNone
    End With
    WS.Range("A2").Select

    Application.ScreenUpdating = True

I'm getting a run time error 5, invalid procedure call or argument and it fails on this line:

VBA Code:
ActiveCell.Hyperlinks.Add Anchor:=Selection, Address:=Path, ScreenTip:=ScreenTip, TextToDisplay:=TextToDisplay

Can anyone advise why and how to resolve it?
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
That code works for me, but you should avoid using VBA terms for the names of variables & procedures.
Try changing the your variable names Path, ScreenTip, TextToDisplay & see if that helps.
 
Upvote 0
Cheers Fluff - I've tried changing the names but I get the same error so I have no idea what's going on.
 
Upvote 0
Make sure you don't have any blank cells in col B.
 
Upvote 0

Forum statistics

Threads
1,215,851
Messages
6,127,307
Members
449,374
Latest member
analystvar

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