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 42 43
|
Sub Test()
Dim Fe As Worksheet
Dim Ctrl As OLEObject
Dim Btn As MSForms.CommandButton
Dim Cel As Range
Set Fe = Worksheets("Feuil1")
'cellule D6
Set Cel = Fe.Cells(6, 4)
'si un second test, détruit le premier bouton
On Error Resume Next
Fe.OLEObjects("MonBouton").Delete
On Error GoTo 0
'crée un bouton sur la cellule D6 de deux fois sa hauteur et deux fois sa largeur
Set Ctrl = Fe.OLEObjects.Add("Forms.CommandButton.1", , , , , , , Cel.Left, Cel.Top, Cel.Width * 2, Cel.Height * 2)
'affecte l'OLEobject à l'objet CommandButton afin d'utiliser certaines
'de ses propriétés comme ici, "Caption", "ForeColor", "Font", etc...
Set Btn = Ctrl.Object
With Btn
.Name = "MonBouton"
.Caption = "Bouton pour Phifou"
.ForeColor = &HC00000
With .Font
.Size = 12
.Bold = True
End With
End With
'affiche les positions des deux (cellule et bouton, ce sont les mêmes)
MsgBox "Left de la cellule : " & Cel.Left & vbCrLf & _
"Top de la cellule : " & Cel.Top & vbCrLf & vbCrLf & _
"Left du bouton : " & Btn.Left & vbCrLf & _
"Top du bouton : " & Btn.Top
End Sub |
Partager