Reference to Command Buttons Added During Runtime with VBA in Excel

前端 未结 2 586
长情又很酷
长情又很酷 2021-01-28 14:55

During runtime, the user is able to add any number of ActiveX command buttons to Sheet 1. I need to have a reference to these new buttons with VBA, but am not sure how.

2条回答
  •  一向
    一向 (楼主)
    2021-01-28 15:43

    Suppose you have a command button (OLE object) with the name 'cmdOriginal' and you want to copy that button and paste it on the same workheet and change the name and caption of the new button into "cmdButtonCopy" and "This is a copy". The newly added button has the highest index in the OLEObjects collection! Place the following code in the code section of the worksheet

    Sub x1()
        Me.OLEObjects("cmdOriginal").Copy
        Me.Paste
        With Me.OLEObjects(Me.OLEObjects.Count)
            .Name = "cmdButtonCopy"
            .Caption = "This is a copy"
        End With
    End Sub
    

提交回复
热议问题