VBA Hyperlink to Main Sheet

benntw

Board Regular
Joined
Feb 17, 2014
Messages
222
Office Version
  1. 365
Platform
  1. Windows
I have a workbook that creates sheets from a list of work order numbers. What I am trying to do is as the code is adding sheets, named as the WO Number, I want it to add a hyperlink in A1 to the main sheet. Here is my code to add sheets from the list of work orders. Hopefully someone can help with this. Thank you.

Sub InsertNamedWorksheets()
Dim MyCell As Range, MyRange As Range

Set MyRange = Sheets("SAP Worklist").Range("E3")
Set MyRange = Range(MyRange, MyRange.End(xlDown))

Application.ScreenUpdating = False
Application.DisplayAlerts = False


For Each MyCell In MyRange
Sheets.Add After:=Sheets(Sheets.Count)
Sheets(Sheets.Count).Name = MyCell.Value
Next MyCell

Application.DisplayAlerts = False
Application.ScreenUpdating = False

End Sub
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Assuming that "SAP Worklist" is your main sheet, try adjusting your For Each loop as follows:

VBA Code:
    For Each MyCell In MyRange
        Sheets.Add After:=Sheets(Sheets.Count)
        Sheets(Sheets.Count).Name = MyCell.Value
        With Sheets(MyCell.Value)
            .Hyperlinks.Add _
            Anchor:=.Range("A1"), _
            Address:="", _
            SubAddress:= _
                "'SAP Worklist'!A1", TextToDisplay:="Main Sheet"
        End With
    Next MyCell
 
Upvote 0
Untested so test on copy:

VBA Code:
Sub InsertNamedWorksheets()
Dim MyCell As Range, MyRange As Range

    Set MyRange = Sheets("SAP Worklist").Range("E3")
    Set MyRange = Range(MyRange, MyRange.End(xlDown))
    
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    
    
    For Each MyCell In MyRange
        Sheets.Add After:=Sheets(Sheets.Count)
        Sheets(Sheets.Count).Name = MyCell.Value
        Sheets(Sheets.Count).Hyperlinks.Add _
        Anchor:=Sheets(Sheets.Count).Range("A1"), SubAddress:="Sheet1!A1", TextToDisplay:="HOME"
    Next MyCell
    
    Application.DisplayAlerts = False
    Application.ScreenUpdating = False

End Sub
 
Upvote 0
Assuming that "SAP Worklist" is your main sheet, try adjusting your For Each loop as follows:

VBA Code:
    For Each MyCell In MyRange
        Sheets.Add After:=Sheets(Sheets.Count)
        Sheets(Sheets.Count).Name = MyCell.Value
        With Sheets(MyCell.Value)
            .Hyperlinks.Add _
            Anchor:=.Range("A1"), _
            Address:="", _
            SubAddress:= _
                "'SAP Worklist'!A1", TextToDisplay:="Main Sheet"
        End With
    Next MyCell


Thank you very much. It worked perfect !
 
Upvote 0
Untested so test on copy:

VBA Code:
Sub InsertNamedWorksheets()
Dim MyCell As Range, MyRange As Range

    Set MyRange = Sheets("SAP Worklist").Range("E3")
    Set MyRange = Range(MyRange, MyRange.End(xlDown))
   
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
   
   
    For Each MyCell In MyRange
        Sheets.Add After:=Sheets(Sheets.Count)
        Sheets(Sheets.Count).Name = MyCell.Value
        Sheets(Sheets.Count).Hyperlinks.Add _
        Anchor:=Sheets(Sheets.Count).Range("A1"), SubAddress:="Sheet1!A1", TextToDisplay:="HOME"
    Next MyCell
   
    Application.DisplayAlerts = False
    Application.ScreenUpdating = False

End Sub

Thank you
 
Upvote 0

Forum statistics

Threads
1,214,428
Messages
6,119,420
Members
448,895
Latest member
omarahmed1

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