Sub essai()
nbouton = 8
Set usf = ThisWorkbook.VBProject.VBComponents.Add(3)
With usf
.Properties('Caption') = 'Avec l'aide de Thierry'
.Properties('Width') = 609
.Properties('Height') = 398
.Properties('ShowModal') = True
End With
With usf.CodeModule
X = .CountOfLines
.insertlines X + 1, 'Private Sub UserForm_Initialize()'
.insertlines X + 2, 'Dim I As Integer, derlign As Integer'
.insertlines X + 3, 'Dim boutoncontrol As Object'
.insertlines X + 4, 'Dim nbouton As Integer'
.insertlines X + 5, 'nbouton = 8'
.insertlines X + 6, 'For I = 1 To nbouton'
.insertlines X + 7, 'Set boutoncontrol = Userform1.Controls.Add(' & Chr(34) & 'Forms.CommandButton.1' & Chr(34) & ',' & Chr(34) & 'bouton' & Chr(34) & '& I)'
.insertlines X + 8, 'boutoncontrol.TakeFocusOnClick = True'
.insertlines X + 9, 'boutoncontrol.Left = 550'
.insertlines X + 10, 'boutoncontrol.Top = 50 + 20 * I'
.insertlines X + 11, 'boutoncontrol.Width = 15'
.insertlines X + 12, 'boutoncontrol.Height = 15'
.insertlines X + 13, 'boutoncontrol.Name =' & Chr(34) & 'bouton' & Chr(34) & '& I'
.insertlines X + 14, 'boutoncontrol.BackColor = &HC0C0C0'
.insertlines X + 15, 'Next I'
.insertlines X + 16, 'End Sub'
End With
With usf.CodeModule
For I = 1 To nbouton
X = .CountOfLines
.insertlines X + 1, 'private Sub Bouton' & I & '_click()'
.insertlines X + 2, 'Msgbox(' & Chr(34) & 'Bouton ' & Chr(34) & '& i)'
.insertlines X + 3, 'End Sub'
Next I
End With
End Sub