How to put a code when you add a commandbutton?

Asked

Viewed 126 times

1

so I don’t really know how to use this, of questions and such. But like, I have a vba sub that when executed creates an object(a button). She’s working on the part of creating it but I need when she creates it, to make it receive 2 modules. Like when we edit the code from a normal button and put the modules to call it. Ex: from a normal button.

Private Sub btnVerifica_Click()

Call Iniciar
Call Teste

End Sub

And that’s my sub that’s creating the new button.

Sub CriarBotao()
    Dim Obj As Object
    Sheets(ActiveSheet.Name).Select

    'Criado o botão
    Set Obj = ActiveSheet.OLEObjects.Add(ClassType:="Forms.CommandButton.1", Link:=False, DisplayAsIcon:=False, Left:=200, Top:=100, Width:=100, Height:=35)
    Obj.Name = "BoutonTest"
    'teste do botão
    ActiveSheet.OLEObjects(1).Object.Caption = "Testar o botão "
End Sub

Only that it does nothing, and I don’t know how to put those two previous modules inside this. Could you help me, please?

1 answer

0

My suggested code:

Sub CriarBotao()

    Sheets(ActiveSheet.Name).Select

    'Criado o botão
    ActiveSheet.Buttons.Add(Left:=200, Top:=100, Width:=100, Height:=35).Select
    Selection.Name = "BoutonTest"
    Selection.Text = "Testar o botão"
    Selection.OnAction = "Clique"

End Sub

Sub Clique()

    Call Iniciar
    Call Teste

End Sub

Browser other questions tagged

You are not signed in. Login or sign up in order to post.