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
 

Some videos you may like

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.

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.
 

Watch MrExcel Video

Forum statistics

Threads
1,118,388
Messages
5,571,842
Members
412,420
Latest member
grace_abar
Top