Copie d'une macro vers un autre fichier
Bonjour je souhaite copier un module "toto" d'un fichier vers un autre fichier.
Malheureusement mon code plante à
Code:
1 2 3
| With ActiveWorkbook.VBProject.VBComponents("toto").CodeModule
S = .Lines(1, .CountOfLines)
End With |
Pouvez vous m'aider?
Voici le code complet:
Code:
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
| Sub MAJCodeModule()
Dim S As String, Wbk As Workbook
tgt = ThisWorkbook.Sheets("Feuil1").Cells(1, 1).Value 'nom fichier excel
'module à copier toto
With ActiveWorkbook.VBProject.VBComponents("toto").CodeModule
S = .Lines(1, .CountOfLines)
End With
Set Wbk = Workbooks(tgt)
'détruire le module à mettre à jour s'il existe
On Error Resume Next
With Wbk.VBProject.VBComponents
.Remove .Item("toto")
End With
On Error GoTo 0
'ajouter un nouveau module et copier le code
Wbk.VBProject.VBComponents.Add(1).Name = "toto"
With Wbk.VBProject.VBComponents("toto").CodeModule
.AddFromString S
End With
End Sub |
Merci d'avance