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 27 28 29
| Sub new_module(shortname)
'insère un module appelé "Mod_btn" dans le fichier résultat
Dim VBComp As VBComponent
Dim x As Integer
Workbooks(shortname).Activate
'Création du module
Set VBComp = ActiveWorkbook.VBProject.VBComponents.Add(1)
'Renomme le module
VBComp.Name = "Mod_btn"
'Code de la macro dans le module
With VBComp.codemodule
x = .CountOfLines
.InsertLines x + 1, "Sub btn1_clic()"
.InsertLines x + 2, "dim a as string"
.InsertLines x + 3, "dim x as integer"
.InsertLines x + 4, "a = ActiveChart.Name"
.InsertLines x + 5, "Worksheets(""temp"").Activate"
.InsertLines x + 6, "x = 1"
.InsertLines x + 7, "Do While Cells(1, x).Value <> Cells(1, x + 1).Value"
.InsertLines x + 8, "x = x + 1"
.InsertLines x + 9, "Loop"
.InsertLines x + 10, "For i = 1 To x - 3"
.InsertLines x + 11, "If Cells(1, i).Value = a Then"
.InsertLines x + 12, "Range(Cells(1, i), Cells(1, i + 3)).Select"
.InsertLines x + 13, "End If"
.InsertLines x + 14, "Next"
.InsertLines x + 15, "End Sub"
End With
End Sub |
Partager