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 30 31 32 33 34 35 36 37 38 39 40 41
| Sub CreerBoutonEtCode()
' Declarer les variables
Dim i As Long, Hght As Long
Dim Name As String, NName As String
' Definir les propriétés du button
i = 0
Hght = 305.25
' Definir le nom du bouton
NName = "cmdAction" & i
' Tester si le bouton existe alors incrementer son nom
For Each OLEObject In ActiveSheet.OLEObjects
If Left(OLEObject.Name, 9) = "cmdAction" Then
Name = Right(OLEObject.Name, Len(OLEObject.Name) - 9)
If Name >= i Then
i = Name + 1
End If
NName = "cmdAction" & i
Hght = Hght + 27
End If
Next
' Ajouter button
Dim myCmdObj As OLEObject, N%
Set myCmdObj = ActiveSheet.OLEObjects.Add(ClassType:="Forms.CommandButton.1", _
Link:=False, DisplayAsIcon:=False, Left:=52.5, Top:=Hght, _
Width:=202.5, Height:=26.25)
' Definir le nom du bouton
myCmdObj.Name = NName
' Define le text du buttons
myCmdObj.Object.Caption = "Cliquer ici"
' Inserer le code du clique pour le bouton
With ThisWorkbook.VBProject.VBComponents(ActiveSheet.CodeName).CodeModule
N = .CountOfLines
.InsertLines N + 1, "Private Sub " & NName & "_Click()"
.InsertLines N + 2, vbNewLine
.InsertLines N + 3, vbTab & "MsgBox(" & """" & "Bouton ok!" & """" & " & vbCrLf &" & _
"""" & "Mettre le code ici!" & """" & " & vbCrLf & " & """" & "This is " & """" & _
"& " & """" & NName & """" & ")"
.InsertLines N + 4, vbNewLine
.InsertLines N + 5, "End Sub"
End With
End Sub |
Partager