Create SelectionChange code to a new sheet via VBA

cbarkho

New Member
Joined
Oct 15, 2021
Messages
5
Office Version
  1. 365
Platform
  1. Windows
Dear Sir,

I have a VBA code that creates a new sheet named One_To_Many.
I want to attach to the newly created sheet One_To_Many via VBA coding a SelectionChange event.

Is this doable?

Sorry for my fresh experience in Excel VBA.

Thanks.

Camille
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
try this
VBA Code:
Sub ADD_CODE()
With ActiveWorkbook.VBProject.VBComponents(Sheets("One_To_Many").CodeName).CodeModule
        N = .CountOfLines
        .InsertLines N + 1, "Private Sub Worksheet_SelectionChange(ByVal Target As Range)"
        .InsertLines N + 2, vbNewLine
        .InsertLines N + 3, "Range(""I1"").Select"
        .InsertLines N + 4, "With Selection.Font"
        .InsertLines N + 5, ".ThemeColor = xlThemeColorDark1"
        .InsertLines N + 6, ".TintAndShade = 0"
        .InsertLines N + 7, "End With"
        .InsertLines N + 8, "End Sub"
 
    End With
End Sub
 
Upvote 0
Hi and Welcome to MrExcel!

You might consider having the desired code available at workbook level in advance.

This goes in the ThisWorkbook module:
VBA Code:
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
    If Sh.Name = "One_To_Many" Then
        OneToManySpecific Target
    End If
End Sub


This goes in a standard module:
VBA Code:
Public Sub OneToManySpecific(ByVal argTarget As Range)
    ' your worksheet specific code goes here
End Sub
 
Upvote 0
Thank you very much, should I keep CodeName and CodeModule or should I replace them with something else. Thanks for baring with me.

Camille
 
Upvote 0
Hi and Welcome to MrExcel!

You might consider having the desired code available at workbook level in advance.

This goes in the ThisWorkbook module:
VBA Code:
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
    If Sh.Name = "One_To_Many" Then
        OneToManySpecific Target
    End If
End Sub


This goes in a standard module:
VBA Code:
Public Sub OneToManySpecific(ByVal argTarget As Range)
    ' your worksheet specific code goes here
End Sub



[QUOTE="GWteB, post: 5771693, member: 158822"]
Hi and Welcome to MrExcel!

You might consider having the desired code available at workbook level in advance.

[SIZE=5]This goes in the [B]ThisWorkbook module[/B]:[/SIZE]
[CODE=vba]Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
    If Sh.Name = "One_To_Many" Then
        OneToManySpecific Target
    End If
End Sub


This goes in a standard module:
VBA Code:
Public Sub OneToManySpecific(ByVal argTarget As Range)
    ' your worksheet specific code goes here
End Sub
Thank you GWteB, I need to place an event listener to certain cells in certain sheets only (those sheets are to be created by the VBA code not manually).
[/QUOTE]
 
Upvote 0
Rather than trying to add code programmatically, why not create a template sheet that has the code & then just copy that sheet?
 
Upvote 0
I need to place an event listener to certain cells in certain sheets only (those sheets are to be created by the VBA code not manually).
I understand both aspects (the need of an event handler & dynamic sheet creation) and the suggested approach is doable and works.
 
Upvote 0
try this
VBA Code:
Sub ADD_CODE()
With ActiveWorkbook.VBProject.VBComponents(Sheets("One_To_Many").CodeName).CodeModule
        N = .CountOfLines
        .InsertLines N + 1, "Private Sub Worksheet_SelectionChange(ByVal Target As Range)"
        .InsertLines N + 2, vbNewLine
        .InsertLines N + 3, "Range(""I1"").Select"
        .InsertLines N + 4, "With Selection.Font"
        .InsertLines N + 5, ".ThemeColor = xlThemeColorDark1"
        .InsertLines N + 6, ".TintAndShade = 0"
        .InsertLines N + 7, "End With"
        .InsertLines N + 8, "End Sub"
 
    End With
End Sub
Thank you very much AC PORTA VIA, it worked flawlessly.
 
Upvote 0
Hi and Welcome to MrExcel!

You might consider having the desired code available at workbook level in advance.

This goes in the ThisWorkbook module:
VBA Code:
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
    If Sh.Name = "One_To_Many" Then
        OneToManySpecific Target
    End If
End Sub


This goes in a standard module:
VBA Code:
Public Sub OneToManySpecific(ByVal argTarget As Range)
    ' your worksheet specific code goes here
End Sub
Thanks GWteB for your perfectly working script too.
 
Upvote 0

Forum statistics

Threads
1,213,504
Messages
6,114,020
Members
448,543
Latest member
MartinLarkin

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