CommandButton VBA
A
Bonjour à tous,
Est ce que vous savez s'il est possible de renommer et colorer un CommandButton ActiveX à l'aide d'une macro ? Créer le button à l'aide d'une macro pas de soucis mais pour le renommer, le colorier ça ne marche pas ... J'aimerais attribuer des lignes de code à ce nouveau button créer à l'aide d'une macro également..
Merci d'avance et bonne journée
Salut Antonio,
Voilà qui devrait t'aider :
Sub AddActiveX()
'Microsoft VBA Extensibility 5.
'Accès approuvé pour les macros
Dim obj As Object, Sh As Worksheet
Set Sh = ThisWorkbook.ActiveSheet
Set obj = Sh.OLEObjects.Add(ClassType:="Forms.CommandButton.1", Link:=False, DisplayAsIcon:=False, Left:=101.25, Top:=38.25, Width:=191.25, Height:=29.25)
With Sh.Shapes(obj.Name).DrawingObject.Object
.Caption = "Cliquer pour voir un coucou!"
.BackColor = RGB(25, 150, 25)
End With
Set vb = ThisWorkbook.VBProject.VBComponents(Sh.Name).CodeModule
vb.InsertLines vb.CountOfLines + 1, "Private Sub " & obj.Name & "_Click ()"
vb.InsertLines vb.CountOfLines + 2, "Msgbox ""Coucou"""
vb.InsertLines vb.CountOfLines + 3, "End sub"
Set obj = Nothing: Set Sh = Nothing
End SubAvec sa tu créé un CommandButton, tu change sa Caption et son BackColor et tu insère automatiquement un code attaché à se bouton.
A dispo
Bonjour,
voici un exemple,
Sub AjoutCommandButton_Feuille()
Dim Ws As Worksheet
Dim Obj As OLEObject
Dim laMacro As String
Dim x As Integer
Set Ws = ActiveSheet
l = 0.75
t = 0.75
w = 60
h = 20.25
For i = 1 To 10
Set Obj = Ws.OLEObjects.Add("Forms.CommandButton.1")
With Obj
.Left = l
.Top = t
.Width = w
.Height = h
.Object.ForeColor = RGB(255, 0, 0) 'Couleur de fond
.Object.BackColor = RGB(100, 150, 217) 'Couleur de fond
.Object.Caption = "CB" & i
End With
'Paramètres pour la création de la macro:
laMacro = "Sub CommandButton" & i & "_Click()" & vbCrLf
laMacro = laMacro & "MsgBox ""CommandButton " & i & vbCrLf
laMacro = laMacro & "End Sub"
With ThisWorkbook.VBProject.VBComponents(ActiveSheet.Name).CodeModule
x = .CountOfLines + 1
.insertlines x, laMacro
End With
l = l + 60
If l = 600.75 Then
l = 0.75
t = t + 20.25
End If
Next
End Sub