Invalid procedure call or argument When creating link

Macro_Nerd99

Board Regular
Joined
Nov 13, 2021
Messages
61
Office Version
  1. 365
I get a Runtime error '5': Invalid procedure call or argument when I run this code:

VBA Code:
    Dim my_pic As Picture
     Set my_pic = ActiveSheet.Pictures(1)

      ActiveSheet.Hyperlinks.Add Anchor:=my_pic, Address:="https://google.com"

What is wrong with this code and how do I fix this error?
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
ActiveSheet is a member of the Application object and I don't think it has a picture property. The only "picture" property I could find belongs to the HeaderFooter class. Then again, you didn't say which line causes the error and that's always important information.
 
Upvote 0
ActiveSheet is a member of the Application object and I don't think it has a picture property. The only "picture" property I could find belongs to the HeaderFooter class. Then again, you didn't say which line causes the error and that's always important information.
I was able to make the picture object and delete it, but I couldn't assign a link to it. So the 3rd line is the error.
 
Upvote 0
Have never tried this. Can only say that there is a subAddress property that I have used to make hyperlinks within workbooks work so maybe try that instead. Otherwise I'm afraid I don't know what else to suggest.
 
Upvote 0
The exact code will depend on how you want to access the picture. In the below code I have assumed that you know the name of the picture.
If there are no other Shapes and you know you want the first one you could use Shapes(1)

VBA Code:
Sub HyperlinkOnPicture()

    Dim my_pic As Shape
    Set my_pic = ActiveSheet.Shapes("Picture 3")
   
    ActiveSheet.Hyperlinks.Add Anchor:=my_pic, Address:= _
        "https://google.com"

End Sub

If you want to continue to use the Pictures Item number as opposed to the Shapes Item number the below works:
The hyperlink needs a Shape object and doesn't seem to accept an picture object.
Looping through shapes picks up pictures but looping through pictures on picks up pictures.

VBA Code:
Sub PicturesByItemNo()
    Dim my_pic As Shape
    With ActiveSheet
        Set my_pic = .Shapes(.Pictures(1).Name)
    End With
    
    ActiveSheet.Hyperlinks.Add Anchor:=my_pic, Address:="https://google.com"
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,213,546
Messages
6,114,251
Members
448,556
Latest member
peterhess2002

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