简体   繁体   English

宏以创建ActiveX命令按钮并将宏分配给该按钮

[英]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 我在这里需要帮助,我正在创建代码,其功能之一就是创建一个activeX命令按钮,但是我需要代码来创建按钮并将宏“ Subgraft()”分配给它

So i need the macro to create the activeX button "Commandbutton1" and assign the code 所以我需要宏来创建activeX按钮“ Commandbutton1”并分配代码

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 我知道如何创建按钮,但是我找不到如何为其分配代码,该按钮将在ActiveWorkbook上名为“Gráfico”的工作表上创建

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. 您将需要稍微修改代码,但这会将按钮放置在您的代码所放置的位置,为按钮提供标题,并为其分配宏“ graft”。

Using With to avoid select statements, then assign the properties of the command button however you see fit. 使用With来避免select语句,然后分配命令按钮的属性(如果您认为合适)。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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