Create event handlers for multiple dynamic controls

前端 未结 2 999
孤街浪徒
孤街浪徒 2020-12-04 01:28

I have a userform that creates two dynamic control buttons but I am having difficulty accessing the .name property of the dynamic control, which means I can\'t

2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-04 01:42

    I got the events for multiple buttons to work with help from .. JWalk Excel Tips

    Below is the modification based on your code and the link provided.

    Create a Class module called "Class1"

    Class1 Module

    Add modified code to UserForm1..

    Option Explicit
    
    Dim Buttons() As New Class1
    
    Private Sub TextBox1_Change()
    Dim i As Integer
    Dim buttonStartPosition As Integer
    Dim cButton As CommandButton
    
    buttonStartPosition = 30
    
    If TextBox1 <> vbNullString Then
     For i = 1 To TextBox1.Value
        Set cButton = Me.Controls.Add("Forms.CommandButton.1")
        With cButton
            .Name = "CommandButton" & i
            .Left = 15
            .Top = buttonStartPosition
            .Width = 30
            .Height = 14
        End With
    
        ReDim Preserve Buttons(1 To i)
        Set Buttons(i).ButtonGroup = cButton
    
        buttonStartPosition = buttonStartPosition + 14
    
     Next i
    End If
    
    End Sub
    

提交回复
热议问题