以编程方式运行“分配宏”对话框

时间:2018-06-04 13:37:01

标签: excel vba excel-vba

我创建的宏工具允许以编程方式在Excel中创建按钮,但最后我需要调用“Assign macro”对话框:

enter image description here

让用户选择他想要分配给创建按钮的宏。我怎么能做到这一点?

实际代码:

Dim btn As Shape
Set btn = wsSheetToAddButton.Shapes.AddFormControl(Type:=xlButtonControl, Left:=buttonLocation.Left, Top:=buttonLocation.Top, Width:=128, Height:=75)
With btn
    With .TextFrame.Characters
        .Caption = buttonText
        With .Font
            .FontStyle = "Bold"
            .ColorIndex = textColor
        End With
    End With
.Select
End With

2 个答案:

答案 0 :(得分:7)

选择按钮后添加:

Application.Dialogs(xlDialogAssignToObject).Show

答案 1 :(得分:3)

如果你想分配"宏"从代码中,这是一个最小的例子:

Sub TestMe()

    Dim btn As Shape
    Set btn = ActiveSheet.Shapes.AddFormControl(Type:=xlButtonControl, _
                                Left:=5, Top:=5, Width:=75, Height:=75)
    btn.OnAction = "Testing"

End Sub

或引用新创建的按钮as mentioned by @Rory

Sub TestMe()

    Dim btn As Shape        
    Set btn = ActiveSheet.Shapes.AddFormControl(Type:=xlButtonControl, _
                                Left:=5, Top:=5, Width:=75, Height:=75)
    btn.Select: Application.Dialogs(xlDialogAssignToObject).Show

End Sub