Bonjour à tous.

Je souhaite, à l'insertion d'une nouvelle feuille, via le code du USF, insérer le code "Private Sub Worksheet_Change(ByVal Target As Range)" dans le module de cette nouvelle feuille, par macro :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
...
ElseIf Feuille.Text = "New Entry" Then
 
    Worksheets.Add.Name = nouveau
    nom = Worksheets(nouveau).CodeName
 
    With ThisWorkbook.VBProject.VBComponents(nom).CodeModule
    X = .CountOfLines
    .InsertLines X + 1, "Private Sub Worksheet_Change(ByVal Target As Range)"
    .InsertLines X + 3, "Dim n as Integer"
    .InsertLines X + 5, "For n = 1 To Worksheets.Count"
    .InsertLines X + 6, "    If Worksheets(n).Name = activesheet.Name Then"
    .InsertLines X + 7, "        Exit For"
    .InsertLines X + 8, "    End If"
    .InsertLines X + 9, "Next"
    .InsertLines X + 11, "If Target.Row <= FirstLine - 1 Then"
    .InsertLines X + 12, "    If Range(""Fin"" & n).Row > FirstLine - 1 Then"
    .InsertLines X + 13, "        activesheet.Rows(Selection.Row).Delete"
    .InsertLines X + 14, "        MsgBox ""You are not allowed to insert rows on this part of the sheet"", vbExclamation, ""Not Allowed"""
    .InsertLines X + 15, "    End If"
    .InsertLines X + 16, "ActiveWorkbook.Names.Add Name:=""Fin"" & n, RefersTo:=ActiveSheet.Range(""A"" & FirstLine - 1)"
    .InsertLines X + 17, "End If"
    .InsertLines X + 19, "End Sub"
    End With
 
End If
A chaque essai, Excel a un problème et doit 'recover' mon fichier et redémarrer.

De quoi cela peut-il provenir ? Je perds patience et je ne comprends pas où est l'erreur, d'autant qu'on ne peut pas exécuter cette partie de code en break mode, donc aucun moyen de savoir.

Quelqu'un a-t-il une idée ?