在动态创建的工作表中创建动态按钮

时间:2019-06-18 07:44:01

标签: excel vba

我已经动态创建了一个新工作表和该工作表中的2个新按钮(不在用户窗体上),如何为这两个新按钮调用click事件?

我只需要2个按钮,我知道它们的名称,但是我无法将button_click代码放置在Microsoft Excel对象中,因为它是动态创建的。我尝试创建一个类模块,但是提示我该对象不支持自动化。

有人可以帮忙吗?

Set ws = ThisWorkbook.Sheets.Add
ws.Name = TableName

ws.Activate

 Set UploadButton = ws.OLEObjects.Add(ClassType:="Forms.CommandButton.1", Link:=False, DisplayAsIcon:=False)

  With UploadButton

    .Name = "UploadButton"
    .Object.Caption = "Upload"
    .Top = 0
    .Left = 0
    .Width = 100
    .Height = 20
    .Placement = xlMoveAndSize
    .PrintObject = True            'or false as per your taste

End With


Set CancelButton = ws.OLEObjects.Add(ClassType:="Forms.CommandButton.1", Link:=False, DisplayAsIcon:=False)

With CancelButton

    .Name = "CancelButton"
    .Object.Caption = "Cancel"
    .Top = 0
    .Left = 120
    .Width = 100
    .Height = 20
    .Placement = xlMoveAndSize
    .PrintObject = True            'or false as per your taste
End With

我想运行这个

    Private Sub UploadButton_Click()
    MsgBox "Co-Cooo!"
End Sub

0 个答案:

没有答案