Affecter une fonction à un commandbutton
Strix
-
Strix -
Strix -
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
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
1 réponse
-
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