2

I have added images to an Excel worksheet and each image has a hyperlink to another worksheet in the same workbook. Unfortunately, if a user renames a destination worksheet, the hyperlink breaks and it currently has to be manually reset. I believe that I can deal with this situation automatically in VBA by deleting the hyperlink and recreating a new one on a Change event. However, is it possible to use the worksheet codename instead of its new name in the Hyperlink so that there's no need to change anything?

5
  • 4
    Have you tried to define a name? Commented Nov 5, 2022 at 19:15
  • I want to let the users rename the destination worksheets to whatever they want and still have the hyperlinks work. At present, this doesn't happen because Excel doesn't update the hyperlink .SubAddress property when the name changes. I was hoping, therefore, that rather than the worksheet name in the hyperlink, I could use the worksheet CodeName which is 'hidden' and doesn't change. Commented Nov 5, 2022 at 19:36
  • 3
    Please, make a research about a named range and you will better understand the above suggestion... Using such a name, no need to use the sheet name, anymore. The named range will include it and it will be updated even if the sheet name is changed. Commented Nov 5, 2022 at 19:48
  • Ah OK. I understand what you mean now. I've used named ranges elsewhere in the workbook and so I'll give it a shot. Commented Nov 5, 2022 at 20:11
  • That seems to have done the trick. Many thanks. Commented Nov 6, 2022 at 0:30

1 Answer 1

1

Activate a Sheet (Using Its Code Name) By Using a Shape

  • If using hyperlinks is not mandatory, you could assign this macro to each of the pictures (shapes).
Sub ActivateSheet()
    
    ' The following two arrays need to have the same number of elements.
    Dim PictureNames() As Variant
    PictureNames = VBA.Array("Picture 1", "Rectangle 1") ' add more, adjust!
    Dim SheetObjects() As Variant
    SheetObjects = VBA.Array(Sheet2, Chart4) ' add more, adjust!
    
    Dim pName As String
    On Error Resume Next
        ' 'Sheet1' is the code name of the worksheet with the pictures. Adjust!
        pName = Sheet1.Shapes(Application.Caller).Name
    On Error GoTo 0
    If Len(pName) = 0 Then Exit Sub ' not called from a shape on 'Sheet1'
    
    Dim Index As Variant: Index = Application.Match(pName, PictureNames, 0)
    If IsError(Index) Then Exit Sub ' doesn't exist in list (array)
    
    ' The arrays are zero-based, while the index is one-based.
    SheetObjects(Index - 1).Activate

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.