Hyperlink to create new Worksheets

Robertson

New Member
Joined
Jul 1, 2005
Messages
5
I am trying to create a hyperlink on a dynamic range of variables that enables me to create a new worksheet and name that worksheet depending on the name of the hyperlink that i have just created. I have used the following code to create the hyperlink:

Sub CreateLinks()

Dim myRng As Range, Cell As Range

Set myRng = Range("c4", Range("c65536").End(xlUp))

For Each Cell In myRng
Cell.Hyperlinks.Add anchor:=Cell, Address:="", SubAddress:="", _
TextToDisplay:=Cell.Value
Next Cell

End Sub
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.

Von Pookie

MrExcel MVP
Joined
Feb 17, 2002
Messages
13,686
I don't think you can do that with hyperlinks. You can create a new workbook with a link, but not add *sheets* to the activeworkbook.

You could just use a Sheet_SelectionChange macro to create the new sheets without worrying about hyperlinks.

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim myRng As Range, NewSh As Worksheet

If Target.Count > 1 Then Exit Sub

Set myRng = Range("C4", Range("C65536").End(xlUp))

'if selected cell is within the range
If Not Intersect(Target, myRng) Is Nothing Then
    'if sheet name does not exist: add sheet
    If SheetExists(Target.Text) = False Then
        Set NewSh = Sheets.Add
        With NewSh
            .Move after:=Sheets(Sheets.Count)
            .Name = Target.Value
        End With
    Else
        'if sheet name already exists: display message, no sheet added
        MsgBox "A sheet with this name already exists." & _
        vbCrLf & vbCrLf & "The sheet will not be created."
    End If
End If

End Sub

Private Function SheetExists(sname) As Boolean
'   Returns TRUE if sheet exists in the active workbook
Dim x As Object
On Error Resume Next
Set x = ActiveWorkbook.Sheets(sname)
If Err = 0 Then SheetExists = True Else SheetExists = False
End Function

If you *really* want the links, you can make it a "blank" link using your current code (just change Cell.Hyperlinks to ActiveSheet.Hyperlinks and you should be good). When you click the link, it also selects the cell and should trigger the above code.
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,164,003
Messages
5,834,828
Members
430,324
Latest member
bosphoruskid

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
Top