且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Excel VBA +如何以编程方式向按钮添加代码

更新时间:2023-01-09 15:37:33

我提供的第一个代码假设有一个工作簿.我现在呈现的代码没有.这样做的局限性在于,如果 arrBttns 丢失,则将重置项目,代码和按钮之间的链接也会丢失,并且必须再次运行 addCodeToButtons 过程

The first code I provided assumes 1 workbook. The code I'm presenting now does not. The limitation of this is that if the arrBttns is lost, the project is reset, the link between the code and the button is lost and the procedure addCodeToButtons has to be run again.

wbNewUnshared 中,使用以下代码创建一个类模块

In the wbNewUnshared, create a class module with the following code

Option Explicit

Public WithEvents cmdButtonSave As MSForms.CommandButton
Public WithEvents cmdButtonDoStuff As MSForms.CommandButton

Private Sub cmdButtonDoStuff_Click()
    'Your code to execut on "Do Stuff" button click goes here
    MsgBox "You've just clicked the Do Stuff button"
End Sub

Private Sub cmdButtonSave_Click()
    'Your code to execut on "Save" button click goes here
    MsgBox "You've just clicked the Save button"

End Sub

wbNewUnshared 中添加具有以下代码的标准模块

In the wbNewUnshared add a standard module with the following code

Option Explicit

Dim arrBttns() As New Class1

Public Sub addCodeToButtons()
    Dim bttn As OLEObject
    Dim ws As Worksheet
    Dim i As Long

    ReDim arrBttns(0)

    'Iterate through worksheets
    For Each ws In ThisWorkbook.Worksheets
        'Iterate through buttons on worksheet
        For Each bttn In ws.OLEObjects
            'Expand arrBttns for valid buttons.
            If bttn.Name = "Save" Or bttn.Name = "DoStuff" Then
                If UBound(arrBttns) = 0 Then
                    ReDim arrBttns(1 To 1)
                Else
                    ReDim Preserve arrBttns(1 To UBound(arrBttns) + 1)
                End If
            End If
            'Link button to correct code
            Select Case bttn.Name
                Case "Save"
                    Set arrBttns(UBound(arrBttns)).cmdButtonSave = bttn.Object
                Case "DoStuff"
                    Set arrBttns(UBound(arrBttns)).cmdButtonDoStuff = bttn.Object
            End Select
        Next bttn
    Next ws

End Sub

wbNewUnshared 中的 ThisWorkbook 模块中添加以下代码,这是将代码添加到打开的工作簿中的按钮上.

In the wbNewUnshared add the following code in the ThisWorkbook module, this is to add the code to the buttons on workbook open.

Option Explicit

Private Sub Workbook_Open()
    Call addCodeToButtons
End Sub

完成按钮添加后,在 wbShared 中添加以下行

In the wbShared add the following line after you're done adding buttons

Application.Run "wbNewUnshared.xlsm!addCodeToButtons"


原始答案

将类模块添加到要添加到您的项目中.


Original Answer

Add a class module to your project to which you add.

Option Explicit

Public WithEvents cmdButton As MSForms.CommandButton  'cmdButton can be an name you like, if changed be sure to also change the Private Sub below

Private Sub cmdButton_Click()
    'Your code on button click goes here
    MsgBox "You just clicked me!"
End Sub

向模块中添加以下代码

Option Explicit

Dim arrBttns() As New Class1 'Change Class1 to the actual name of your classmodule

'The sub which adds a button
Sub addButton()
    Dim bttn As OLEObject
    Dim ws As Worksheet

    Set ws = ThisWorkbook.Worksheets("Sheet1")
    Set bttn = ws.OLEObjects.Add(ClassType:="Forms.CommandButton.1")
    ReDim arrBttns(0)

    If UBound(arrBttns) = 0 Then
        ReDim arrBttns(1 To 1)
    Else
        ReDim Preserve arrBttns(1 To UBound(arrBttns))
    End If

    Set arrBttns(UBound(arrBttns)).cmdBttn = bttn.Object

End Sub