object array or collection in VBA Excel

前端 未结 2 662

i would like to have an array of objects in excel that call one event handler. specifically I have multiple buttons which perform the same function to different cells, and

2条回答
  •  囚心锁ツ
    2020-12-16 02:33

    There are no control arrays in VBA like there are in VB. For certain controls you can create a custom class to handle the events. For example, assume you have a userform with two command buttons. In the userform module, put this code

    Private mcolEventButtons As Collection
    
    Private Sub UserForm_Initialize()
    
        Dim clsEventButton As CEventButton
    
        Set mcolEventButtons = New Collection
    
        Set clsEventButton = New CEventButton
        Set clsEventButton.EventButton = Me.CommandButton1
        clsEventButton.RangeAddress = "A1"
        mcolEventButtons.Add clsEventButton, Me.CommandButton1.Name
    
        Set clsEventButton = New CEventButton
        Set clsEventButton.EventButton = Me.CommandButton2
        clsEventButton.RangeAddress = "A10"
        mcolEventButtons.Add clsEventButton, Me.CommandButton2.Name
    
    End Sub
    

    Then create a custom class module named CEventButton and put this code

    Private WithEvents mctlEventButton As MSForms.CommandButton
    Private msRangeAddress As String
    
    Public Property Set EventButton(ctlButton As MSForms.CommandButton)
    
        Set mctlEventButton = ctlButton
    
    End Property
    
    Public Property Get EventButton() As MSForms.CommandButton
    
        Set EventButton = mctlEventButton
    
    End Property
    
    Private Sub mctlEventButton_Click()
    
        Sheet1.Range(Me.RangeAddress).Value = "Something"
    
    End Sub
    
    Public Property Get RangeAddress() As String
    
        RangeAddress = msRangeAddress
    
    End Property
    
    Public Property Let RangeAddress(ByVal sRangeAddress As String)
    
        msRangeAddress = sRangeAddress
    
    End Property
    

    The WithEvents keyword in the variable dimensioning polls the commandbutton's events and fires just as if it was tied to a particular control and in the userform module.

    Here's what you did: You created a Collection to hold instances of your custom class for as long as the userform is active. This ensures that those instances stay in scope. Then you created a new instance of the class, assigned a particular button to it, and saved it in the collection. You did the same for the next button. In this example there are only two buttons, but if you had more you could continue doing this until you run out of memory.

    I create a RangeAddress property in the custom class module as an example. What information you'll need to store will depend on what you're ultimately trying to accomplish.

    For this example to work, you need to set the ShowModal property of the userform to FALSE, have to commandbuttons named CommandButton1 and CommandButton2, have a sheet with a codename of Sheet1, and probably some other stuff.

提交回复
热议问题