excel visual basic命令button链接

我已经使用这个在代码中创build了一个命令button:

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 

但是我希望能够点击button时做些事情。 我做到了这一点:

 Sub CancelButton_Click() UserForm1.Name = "Closed" End Sub 

这不起作用,事件从未运行。 所有这些都在表单代码中运行。 我有初始化等,但这是一个自定义函数。 它创build并显示button,但不会让我运行一个事件,当它的点击。

我之后是取消button被点击时closures表单。

首先,您需要将您的代码放在UserForm 。 然后你需要添加一个类来处理所有的button点击事件。 如果你search“vba用户表单添加控件运行时”,你会发现一些很好的答案,甚至一些在这里。 以下是您为特定情况所做的工作:

首先在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属性来区分实际触发事件的button。

现在您需要将修改后的代码添加到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 

我们声明两个公共variables,一个保存我们刚刚创build的类的实例,一个保存用户窗体生命期的类实例的集合。 在UserForm_Activate事件中,我们为每个button实例化类的新实例,并将其添加到集合中。

然后,运行表单并单击button。

编辑:这是您的要求添加ComboBox的组合ComboBox的响应。 此代码将一个ComboBox添加到clsButton并更改Enterbutton以显示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 

表单代码被更改为创buildcombobox,填充一些值,并将select设置为其项目。 然后当Enterbutton被创build时, cbo属性被设置为它的类实例。 取消button代码不变:

 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,并且添加了Enterbutton的类实例,这样btn就可以“交谈”了。