简体   繁体   中英

Macro to create activex command button and assign a macro to that button

i need a help here, i'm creating a code and one of it's functions will be to create a activeX command button but i need the code to create the button and assign the macro "Sub graft()" to it

So i need the macro to create the activeX button "Commandbutton1" and assign the code

Private Sub CommandButton1_Click()    
Call graft()    
End sub

i know how to create the button, but i can't find how to assign a code to it the button will be created on a worksheet called "Gráfico" on the ActiveWorkbook

Code of creation and placement of the button

ActiveSheet.OLEObjects.Add(ClassType:="Forms.CommandButton.1", Link:=False _
, DisplayAsIcon:=False, Left:=1200, Top:=20, _
Width:=100, Height:=30).Select

ActiveSheet.Shapes.Range(Array("CommandButton1")).Select
Selection.Cut
Cells(lline + 26, 1).Select
ActiveSheet.Paste

CutCopyMode = False

Try this out. You will have to modify your code a little, but this places the button where your code is placing it, gives the button a caption, and assigns the macro "graft" to it.

Using With to avoid select statements, then assign the properties of the command button however you see fit.

Sub CreateButton()

    Dim newButton As Object 
    Set newButton = ActiveSheet.Buttons.Add(1200, 20, 100, 30)

    With newButton
        .OnAction = "graft"
        .Caption = "Graft"
    End With

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM