All,

I have the below code to paste a code snippet in the programmatically created new worksheet's WORKSHEET (as this code is a worksheet change event)module. But its not working properly, sometimes giving error / sometimes pasting in the previously created sheets. My requirement is, whenever we create a new worksheet using another macro, the worksheet_change event code snippet should be pasted in the newly created worksheet's worksheet module.

Private Sub Workbook_NewSheet(ByVal Sh As Object)
Dim wsNew As Worksheet
Dim wsData As Worksheet

 Dim lastAddedSheet As Worksheet
    Dim oneSheet As Worksheet
    With ThisWorkbook
        Set lastAddedSheet = .Sheets(1)
        For Each oneSheet In .Sheets
            If Val(Mid(oneSheet.Name, 6)) > Val(Mid(lastAddedSheet.Name, 6)) Then
                Set lastAddedSheet = oneSheet
            End If
        Next oneSheet
    End With
    MsgBox lastAddedSheet.Name & " was last added."

Set wsNew = ActiveWorkbook.Sheets(lastAddedSheet.Name)


Code = "Private Sub Worksheet_Change(ByVal Target As Range)" & vbCrLf
Code = Code & "Application.EnableEvents = False" & vbCrLf
Code = Code & "Range(""H7"") = Now()" & vbCrLf
Code = Code & "Application.EnableEvents = True" & vbCrLf
Code = Code & "End Sub"


With ThisWorkbook.VBProject.VBComponents(wsNew.CodeName).CodeModule
NextLine = .CountOfLines + 1
.InsertLines NextLine, Code
End With
End Sub
Thanks