简体   繁体   中英

VBA Excel: dynamically created button in a class module runs immediately and only once

I am trying to make class that would create a button. The button should have a macro assigned to it. The macro is a function of the class.

The code of the class module is the following:

'Class Module: btnClass
Option Explicit

Dim btn As Button

Function addButton()
    'Adding a button
    Set btn = ActiveSheet.Buttons.Add( _
        Range("A1").Left, _
        Range("A1").Top, _
        Range("A1").Width, _
        Range("A1").Height)
    With btn
        'Assigning a function
        .OnAction = Me.onClickAction
        .Caption = "Button"
    End With
End Function

Function onClickAction()
    MsgBox ("Click")
End Function

The code of the main macro is the following:

'Module
Option Explicit
Sub main()
    Dim btnInstance As btnClass
    Set btnInstance = New btnClass

    'Calling a function of the instance that creates a button
    Call btnInstance.addButton
End Sub

The code above creates a button successfully. However, the function assigned to the button is run immediately (right after the button is created, not when I click on it), and only once (when you click on the button later, nothing happens).

Is there a way in VBA to implement the required functionality using class modules (want to create a class that does not rely on the outside functions)?

To expand on Rory's comment, your class needs something like this:

Public WithEvents Button As CommandButton

Private Sub Class_Initialize()
    Set Me.Button = Sheet1.OLEObjects("Thebutton").Object
End Sub

Private Sub Button_Click()
    MsgBox "Foo"
End Sub

Then in a normal module create a public instance of the class so it stays in memmory:

Public myButt As ButtonClass

Public Sub AddEvent()
    Set myButt = New ButtonClass
End Sub

Note that the click event will only be handled as long as the instance of the class remains in memory. If you close the workbook and open it again the event will no longer be handled.

Edit: I forgot to mention, you need to set a reference to Microsoft Forms in order to declare a variable of type CommandButton.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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