Option Explicit
Public Sub MaSub()
Dim Shp As Excel.Shape
With Excel.ActiveSheet
For Each Shp In .Shapes
Shp.Delete
Next Shp
AddButton .Cells(1, 1), "Bouton 1", "UneMacro", "Salut" 'fonctionne bien
AddButton .Cells(3, 1), "Bouton 2", "UneMacro", "ça va ?" 'fonctionne bien
AddButton .Cells(5, 1), "Bouton 3", "UneMacro", "Oui merci.", "Et toi ?" 'fonctionne bien
AddButton .Cells(7, 1), "Bouton 4", "UneMacro", "C'est cool" 'Genère une erreur
End With
End Sub
Private Sub AddButton(ByRef Anchor As Excel.Range, ByRef Caption As String, ByRef OnActionSub As String, ParamArray SubArgs() As Variant)
With Anchor
With .Parent.Buttons.Add(.Left + 1, .Top + 1, .Width - 1, .Height - 1)
.Caption = Caption
.OnAction = "'" & VBA.Trim(OnActionSub) & """" & VBA.Join(SubArgs, """, """) & """'"
End With
End With
End Sub
Public Sub UneMacro(Optional ByRef Texte1 As String, Optional ByRef Texte2 As String)
VBA.MsgBox Texte1 & VBA.IIf(Texte2 = VBA.vbNullString, VBA.vbNullString, VBA.vbNewLine & Texte2)
End Sub