VBA Command To Add A Link To A Shape

MikeG

Well-known Member
Joined
Jul 4, 2004
Messages
845
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I have code that creates a new rectangle and I would like to add a hyperlink to the shape.

The code is attached to a button in a sheet called Master and the code creates a new shape in a sheet called Sheet A. The name created for the shape is stored in a variable called NewName.

Set MyShape = Sheets(Sheet A).Shapes.AddShape(msoShapeRectangle, BFL, BT, BW, BH)
MyShape.Name = NewName

Could someone give me the code that would add a hyperlink to the new button so that it would link to Cell A1 in a sheet I have named Products?

Thanks

Mike
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
You don't need a hyperlink, just an extra macro. Extend your code as per below:
VBA Code:
Set MyShape = Sheets(Sheet A).Shapes.AddShape(msoShapeRectangle, BFL, BT, BW, BH)
MyShape.Name = NewName
MyShape.OnAction = "Jump"

The extra macro would look like this:
VBA Code:
Public Sub Jump()
    Application.Goto Reference:=Worksheets("Products").Range("A1")
End Sub
 
Upvote 0
You don't need a hyperlink, just an extra macro. Extend your code as per below:
VBA Code:
Set MyShape = Sheets(Sheet A).Shapes.AddShape(msoShapeRectangle, BFL, BT, BW, BH)
MyShape.Name = NewName
MyShape.OnAction = "Jump"

The extra macro would look like this:
VBA Code:
Public Sub Jump()
    Application.Goto Reference:=Worksheets("Products").Range("A1")
End Sub
Thanks GWteB.

Unfortunately my client won't accept a workbook with any macros in and want to just have hyperlinks, so I am looking for the code to create the hyperlinks, and once that is done, I will remove all macros from the workbook.
 
Upvote 0
Something like this perhaps.

VBA Code:
Sub AddShape()
 Dim WS As Worksheet
    Dim MyShape As Shape
    Dim HL As Hyperlink
    Dim BFL, BT, BW, BH
    Dim NewName As String
    
    
    Set WS = Worksheets("Sheet A")
    NewName = "NewShape"
    
    BFL = 100
    BT = 500
    BW = 80
    BH = 30
    
    Set MyShape = WS.Shapes.AddShape(msoShapeRectangle, BFL, BT, BW, BH)
    MyShape.Name = NewName
    Set HL = WS.Hyperlinks.Add(Anchor:=MyShape, Address:="", SubAddress:="Products!A1")
End Sub
 
Upvote 0
Something like this perhaps.

VBA Code:
Sub AddShape()
 Dim WS As Worksheet
    Dim MyShape As Shape
    Dim HL As Hyperlink
    Dim BFL, BT, BW, BH
    Dim NewName As String
   
   
    Set WS = Worksheets("Sheet A")
    NewName = "NewShape"
   
    BFL = 100
    BT = 500
    BW = 80
    BH = 30
   
    Set MyShape = WS.Shapes.AddShape(msoShapeRectangle, BFL, BT, BW, BH)
    MyShape.Name = NewName
    Set HL = WS.Hyperlinks.Add(Anchor:=MyShape, Address:="", SubAddress:="Products!A1")
End Sub
Thanks! Perfect - just what I was looking for.
 
Upvote 0
Thanks! Perfect - just what I was looking for.
I wonder if you could help me on a follow up.

In the last part of the code I changed the line:

SubAddress:="Products!A1"

to

SubAddress:="Products Two!A1"

and the macro no longer worked., even though the sheet Products Two exists. I did some more experiments, and any time the sheet name has a space, the macro does not work.

Is there something I am doing incorrectly?

Thanks again
 

Attachments

  • 1634948681981.png
    1634948681981.png
    693 bytes · Views: 5
Upvote 0
I wonder if you could help me on a follow up.

In the last part of the code I changed the line:

SubAddress:="Products!A1"

to

SubAddress:="Products Two!A1"

and the macro no longer worked., even though the sheet Products Two exists. I did some more experiments, and any time the sheet name has a space, the macro does not work.

Is there something I am doing incorrectly?

Thanks again
Just realized that it is necessary to add single quotes around when the name has a space! So I'm all set. Appreciate it.
 
Upvote 0
Glad we could help & thanks for the follow-up (y)
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,557
Members
449,088
Latest member
davidcom

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