excelnewbie990
New Member
- Joined
- Jun 30, 2024
- Messages
- 3
- Office Version
- 365
- Platform
- Windows
- MacOS
I have a workbook with a template sheet and a mainsheet.
Is there a way that when I double click on cell in the mainsheet it:
Would appreciate any guidance on how to achieve this?
This is my current code
Is there a way that when I double click on cell in the mainsheet it:
- creates a new sheet from the template I have
- displays the value (number) in the hyperlink that would be entered in the new sheet in cell C15 for example?
- Would update the main sheet if the value in C15 changes(new sheet)
Would appreciate any guidance on how to achieve this?
This is my current code
VBA Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Dim ws As Worksheet
Dim templateSheet As Worksheet
Dim userText As String
' Check if the double-clicked cell is in column C
If Target.Column = 3 Then
' Set reference to the template sheet
Set templateSheet = Me.Parent.Worksheets("template") ' template sheet name
' Copy the template sheet
templateSheet.Copy After:=Me.Parent.Worksheets(Me.Parent.Worksheets.Count)
' Set reference to the new sheet which is a copy of the template
Set ws = Me.Parent.Worksheets(Me.Parent.Worksheets.Count)
' Get the user-entered text from cell C15 of the new sheet
userText = ws.Range("C15").Value
' Add Hyperlink
Target.Hyperlinks.Add Anchor:=Target, Address:="", SubAddress:=ws.Cells(1, 1).Address(True, True, , True), TextToDisplay:=userText
' Set font size of the hyperlink
Target.Font.Size = 10
Target.Font.Color = 1
Target.Font.Bold = 1
' Set the value of the double-clicked cell to the user-entered text
Target.Value = userText
' Disable original Doubleclick action
Cancel = True
End If
End Sub