excel visual basic命令按钮链接

excel visual basic命令按钮链接,excel,spreadsheet,vba,Excel,Spreadsheet,Vba,我使用以下命令从代码内部创建了一个命令按钮: 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

我使用以下命令从代码内部创建了一个命令按钮:

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
这不起作用,因为事件从未运行。 所有这些都在表单代码中运行。我有初始化等,但这是一个自定义函数。它创建并显示按钮,但单击时不允许我运行事件


我想要的是,当单击“取消”按钮时,它会关闭表单。

首先,您需要将代码放入
用户表单中。然后,您需要添加一个类来处理所有按钮单击事件。如果您搜索“vba用户表单添加控件运行时”,您会找到一些很好的答案,甚至在这里也会找到一些。以下是您针对特定情况所做的工作:

首先在VBE中,插入一个新的
类模块
,并将其称为“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
属性,或者使用
标记
属性来区分实际触发事件的按钮

现在,您需要将修改后的代码添加到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的生命周期中保存类实例的集合。在UserForm_Activate事件中,我们为每个按钮实例化一个新的类实例,并将其添加到集合中

然后运行表单并单击按钮

编辑:下面是对您向混音中添加
组合框的请求的响应。此代码将组合框添加到
clsButton
并更改
Enter
按钮以显示组合框的当前值:

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
表单代码被修改以创建组合框,用一些值填充它,并将选择设置为its项。然后,当创建
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

因此,总而言之,我们在类中添加了一个组合框,并将其添加到Enter按钮的类实例中,以便btn可以与之“对话”。

首先,您需要将代码放在
用户表单中。然后,您需要添加一个类来处理所有按钮单击事件。如果您搜索“vba用户表单添加控件运行时”,您会找到一些很好的答案,甚至在这里也会找到一些。以下是您针对特定情况所做的工作:

首先在VBE中,插入一个新的
类模块
,并将其称为“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
属性,或者使用
标记
属性来区分实际触发事件的按钮

现在,您需要将修改后的代码添加到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的生命周期中保存类实例的集合。在UserForm_Activate事件中,我们为每个按钮实例化一个新的类实例,并将其添加到集合中

然后运行表单并单击按钮

编辑:下面是对您向混音中添加
组合框的请求的响应。此代码将组合框添加到
clsButton
并更改
Enter
按钮以显示组合框的当前值:

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
表单代码被修改以创建组合框,用一些值填充它,并将选择设置为its项。然后,当创建
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

总之,我们在类中添加了一个组合框,并将其添加到Enter按钮的类实例中,这样btn就可以与之“对话”。

替换为:Unload Methe事件没有触发,所以我不能这样做。我不明白为什么它不响替换为:Unload meth事件没有触发,所以我不能这样做。我不明白为什么它不起作用这已经很好地工作了!谢谢你的帮助,你解决了我最头疼的问题之一!谢谢你的代码,我在混音中添加了一个组合框,当按下按钮时,我如何阅读你的代码,我用谷歌搜索了一下,尝试了一下,但没有成功。我知道它会被放在if语句中,但我不确定如何获得combobox值。提前谢谢这很有效!谢谢你的帮助,你解决了我最头疼的问题之一!谢谢你的代码,我在混音中添加了一个组合框,当按下按钮时,我如何阅读你的代码,我用谷歌搜索了一下,尝试了一下,但没有成功。我知道它会被放在if语句中,但我不确定如何获得combobox值。提前谢谢