我使用以下代码在代码中创建了一个命令按钮:
Public Sub createForm(label As String)
Dim control As control
Dim controlbutton As CommandButton
'set control for the first label on the page
Set control = UserForm1.Controls.Add("Forms.Label.1", "Text", True)
With control
.Caption = label
.Left = 25
.Top = 10
.Height = 20
.Width = 200
.Visible = True
End With
'set control for the enter button
Set controlbutton = UserForm1.Controls.Add("Forms.CommandButton.1", "Enter", True)
With controlbutton
.Caption = "Enter"
.Name = "Enter"
.Left = 45
.Top = 80
.Height = 30
.Width = 50
.Visible = True
End With
'set control for the cancel button
Set controlbutton = UserForm1.Controls.Add("Forms.CommandButton.1", "CancelButton", True)
With controlbutton
.Caption = "Cancel"
.Left = 105
.Top = 80
.Height = 30
.Width = 50
.Visible = True
End With
'UserForm1.Controls.Add "Forms.TextBox.1", "Name1", True
'UserForm1!Name1.Text = "Hi"
End Sub
但是我希望能够在单击按钮时执行某些操作。我这样做了:
Sub CancelButton_Click()
UserForm1.Name = "Closed"
End Sub
这不起作用,因为事件从未运行过。 所有这些都在表单代码中运行。我有初始化等,但这是一个自定义功能。它创建并显示按钮,但不会让我在点击时运行一个事件。
我所追求的是当点击取消按钮时它会关闭表格。
答案 0 :(得分:3)
首先,您需要将代码放在UserForm
内。然后,您需要添加一个将处理所有按钮单击事件的类。如果您搜索“vba userform add controls runtime”,您会找到一些好的答案,甚至是SO上的一些答案。以下是您针对特定情况所做的事情:
首先在VBE中插入一个新的Class module
并将其命名为“ clsButton ”。在此模块中,您将添加以下代码:
Public WithEvents btn As MSForms.CommandButton
Private Sub btn_Click()
If btn.Caption = "Cancel" Then
MsgBox "Cancel"
ElseIf btn.Caption = "Enter" Then
MsgBox "Enter"
End If
End Sub
WithEvents关键字声明一个btn
对象,该对象在单击时触发事件。您可以使用Caption
属性(如上所述)或Tag
属性来区分实际触发事件的按钮。
现在您需要将修改后的代码添加到UserForm:
Public cButton As clsButton
Public coll As New Collection
Private Sub UserForm_Activate()
Dim controlbutton As CommandButton
Set controlbutton = Me.Controls.Add("Forms.CommandButton.1", "Enter", True)
With controlbutton
.Caption = "Enter"
.Name = "Enter"
.Left = 45
.Top = 80
.Height = 30
.Width = 50
.Visible = True
Set cButton = New clsButton
Set cButton.btn = controlbutton
coll.Add cButton
End With
Set controlbutton = Me.Controls.Add("Forms.CommandButton.1", "CancelButton", True)
With controlbutton
.Caption = "Cancel"
.Left = 105
.Top = 80
.Height = 30
.Width = 50
.Visible = True
Set cButton = New clsButton
Set cButton.btn = controlbutton
coll.Add cButton
End With
End Sub
我们声明了两个公共变量,一个用于保存我们刚创建的类的实例,另一个用于保存用户表单生命周期的类实例。在UserForm_Activate事件中,我们为每个按钮实例化一个新的类实例,并将其添加到集合中。
然后只需运行表单并单击按钮。
编辑:以下是对您添加ComboBox
的请求的回复。此代码将一个ComboBox添加到clsButton
并更改Enter
按钮以显示ComboBox的当前值:
Public WithEvents btn As msforms.CommandButton
Public cbo As msforms.ComboBox
Private Sub btn_Click()
If btn.Caption = "Cancel" Then
MsgBox "Cancel"
ElseIf btn.Caption = "Enter" Then
MsgBox cbo.Value
End If
End Sub
更改表单代码以创建ComboBox,用一些值填充它,并将选择设置为其项目。然后,当创建Enter
按钮时,为其类实例设置cbo
属性。取消按钮代码保持不变:
Public cButton As clsButton
Public coll As New Collection
Private Sub UserForm_Activate()
Dim controlButton As msforms.CommandButton
Dim controlCombo As msforms.ComboBox
Dim i As Long
Set controlCombo = Me.Controls.Add("Forms.ComboBox.1", "Combo", True)
With controlCombo
For i = 1 To 10
.AddItem i
Next i
.ListIndex = 0
End With
Set controlButton = Me.Controls.Add("Forms.CommandButton.1", "Enter", True)
With controlButton
.Caption = "Enter"
.Name = "Enter"
.Left = 45
.Top = 80
.Height = 30
.Width = 50
.Visible = True
Set cButton = New clsButton
Set cButton.btn = controlButton
Set cButton.cbo = controlCombo
coll.Add cButton
End With
Set controlButton = Me.Controls.Add("Forms.CommandButton.1", "CancelButton", True)
With controlButton
.Caption = "Cancel"
.Left = 105
.Top = 80
.Height = 30
.Width = 50
.Visible = True
Set cButton = New clsButton
Set cButton.btn = controlButton
coll.Add cButton
End With
End Sub
因此,总而言之,我们在类中添加了一个ComboBox,并将其添加到Enter按钮的类实例中,以便btn可以与它“对话”。