Affecter une fonction à un commandbutton

Fermé
Strix - Modifié par pijaku le 18/12/2015 à 07:45
 Strix - 18 déc. 2015 à 12:02
Bonjour,

Je vous prie de m'aider à affecter une fonction à un CommandButton que je viens de créer dans une nouvelle feuille = "Data" en VBA. Voici mon code.Et Merci


Sub Remplir_tab(K As Integer)
Dim i As Integer
Dim Obj As Object
ThisWorkbook.Sheets("Data").Activate
With ThisWorkbook.Sheets("Data")

.Range(.Cells(1, 1), .Cells(K + 2, K + 2)).Select
End With


Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
For i = 0 To K
ThisWorkbook.Sheets("Data").Cells(1, i + 2).Value = i
ThisWorkbook.Sheets("Data").Cells(i + 2, 1).Value = i
Next
ThisWorkbook.Sheets("Data").Cells(K + 2, 1).Value = "Distance"

ThisWorkbook.Sheets("Data").Range("A1").Select
Set Obj = ActiveSheet.OLEObjects.Add(ClassType:="Forms.CommandButton.1", _
Link:=False, DisplayAsIcon:=False, Left:=ActiveCell.Left, Top:=ActiveCell.Top, Width:=ActiveCell.Width, _
Height:=ActiveCell.Height)
Obj.Name = "Activation"
ActiveSheet.OLEObjects(0).Object.Caption = "Activer"
'assigne le code au bouton "Delete"
ButtonMacro = "Sub Activation_Click()" & vbCrLf
ButtonMacro = ButtonMacro & "ActiveSheet.Activer" & vbCrLf
ButtonMacro = ButtonMacro & "End Sub"


End Sub
A voir également:

1 réponse

f894009 Messages postés 17185 Date d'inscription dimanche 25 novembre 2007 Statut Membre Dernière intervention 15 avril 2024 1 701
18 déc. 2015 à 09:52
Bonjour,

code pour definition et affectation macro a l'objet cree:

    ThisWorkbook.Sheets("Data").Range("A1").Select
    Set Obj = ActiveSheet.OLEObjects.Add(ClassType:="Forms.CommandButton.1", _
                Link:=False, DisplayAsIcon:=False, Left:=ActiveCell.Left, Top:=ActiveCell.Top, Width:=ActiveCell.Width, _
                Height:=ActiveCell.Height)
    With Obj
            .Name = "Activation"
            .Object.Caption = "Activer"
    End With

    'assigne le code au bouton "Delete" ??????
    ButtonMacro = "Sub Activation_Click()" & vbCrLf
    ButtonMacro = ButtonMacro & "ActiveSheet.Activate" & vbCrLf
    ButtonMacro = ButtonMacro & "End Sub"
 
    With ThisWorkbook.VBProject.VBComponents(ActiveSheet.Name).CodeModule
            X = .CountOfLines + 1
            .InsertLines X, ButtonMacro
    End With
0
Bonjour,
Je vous remercie pour votre réponse. le commentaire est mal placé (à ignorer) .quand je fais tourner mon programme, il m'affiche erreur d'exécution'1004'.avez vous une idée pourquoi ?
0
f894009 Messages postés 17185 Date d'inscription dimanche 25 novembre 2007 Statut Membre Dernière intervention 15 avril 2024 1 701 > Strix
18 déc. 2015 à 11:53
Re,

il m'affiche erreur d'exécution'1004' Oui, et y a pas de debug possible ???
0
Mon probleme c'est que je veux affecter une fonction définie au commandbutton "activation"crée c'est à qd je vais clique sur ce bouton je veux qu'il effectue une opération . Mais je sais pas comment le faire.
0