2 回答
TA贡献1886条经验 获得超2个赞
这是一个例子。请修改它以满足您的需求:)
此示例将创建一个命令按钮并为其分配代码,以便在按下它时,它将显示“Hello World”。
将此代码粘贴到命令按钮的单击事件中,该按钮将动态创建新的命令按钮并为其分配代码。
Option Explicit
Dim cmdArray() As New Class1
Private Sub CommandButton1_Click()
Dim ctl_Command As Control
Dim i As Long
i = 1
Set ctl_Command = Me.Controls.Add("Forms.CommandButton.1", "CmdXYZ" & i, False)
With ctl_Command
.Left = 100
.Top = 100
.Width = 255
.Caption = "Click Me " & CStr(i)
.Visible = True
End With
ReDim Preserve cmdArray(1 To i)
Set cmdArray(i).CmdEvents = ctl_Command
Set ctl_Command = Nothing
End Sub
并将此代码粘贴到类模块中
Option Explicit
Public WithEvents CmdEvents As MSForms.CommandButton
Private Sub CmdEvents_Click()
MsgBox "Hello Word"
End Sub
添加回答
举报