1

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
2
  • Where on Grafico are you wanting to place it? Commented Dec 17, 2014 at 15:17
  • @PJRosenburg i've updated the description with the code that i used to create the buttons, and place them Commented Dec 18, 2014 at 9:56

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.