vba动态创建复选框事件

时间:2018-04-10 11:46:31

标签: excel vba checkbox dynamic userform

这里有一个关于如何使用动态创建的选项按钮和VBA中的复选框进行监视/操作的问题。

我在这里和那里看过许多地方但是没有一个可以适应我的解决方案正在工作(我可能在调整代码时错过了一些东西)。

这里的要点是启用(或禁用)与其复选框相关的选项按钮。 一旦我完成了这一切,如果一切顺利的话,剩下的就很容易了。

我在Userform中的代码:

lst = [(2, 'Operation.SUBSTITUTED'), (1, 'Operation.DELETED'), (2, 'Operation.INSERTED')]

res_int = sorted(lst, key=lambda x: x[1], reverse=True)
res = sorted(res_int, key=lambda x: x[0])

print(res)

# [(1, 'Operation.DELETED'), (2, 'Operation.SUBSTITUTED'), (2, 'Operation.INSERTED')]

End Sub

并在Classe1模块中:

Private Sub userform_initialize()


Label2.Caption = Chr(10) & _
                    "Cochez les paramètres nécessaires pour le DOTE générique" & Chr(10) & _
                    "Le programme se base sur le classeur, et la liste de parapètres possibles sur la feuille 'Diversité', sur la ligne 2." & Chr(10) & _
                    "Pour plus d'informations, sélectionnez l'onglet ''Informations sur les paramètres''."
Label1.Caption = "Infos sur paramètres"
bin = 0
Dim btn_1, btn_0, btn_ok, box, fenetre As Object
j = 0

ActiveWorkbook.Sheets("Diversité").Select
For i = 6 To 32
Cells(2, i).Select
    If Cells(2, i) <> "" And Not Cells(2, i) Like "*serv*" And Not (Cells(2, i) Like "*ctet*") Then
        j = j + 1
        nb_param = j + 1
    End If
Next

j = 0
k = 0
Dim param As String

For i = 6 To 32
    If Cells(2, i) <> "" And Not Cells(2, i) Like "*serv*" And Not (Cells(2, i) Like "*ctet*") Then
    Cells(2, i).Select
        If j = 3 Then
        k = k + 1
        j = 0
        End If

        param = Cells(2, i).Value


        Set fenetre = MultiPage1.page1.Controls.Add("forms.frame.1", "frame_" & param, True)
        fenetre.Caption = param
        fenetre.Top = Label2.Top + Label2.Height + 5 + k * 50
        fenetre.Width = Label2.Width / 3 - 20
        fenetre.Left = 20 + j * fenetre.Width
        fenetre.Height = 45

        'fenerte.BackColor = 0

        With fenetre

        Set btn_1 = .Controls.Add("forms.optionbutton.1", "opt_btn1_" & param, True)
        btn_1.Caption = "1"
        btn_1.Top = btn_1.Top + 5
        btn_1.Left = btn_1.Left + 10
        btn_1.Height = 15
        btn_1.Width = 22.5
        btn_1.GroupName = param
        btn_1.Enabled = False

        Set btn_0 = .Controls.Add("forms.optionbutton.1", "opt_btn0_" & param, True)
        btn_0.Caption = "0"
        btn_0.Top = btn_0.Top + 20
        btn_0.Left = btn_0.Left + 10
        btn_0.Height = 15
        btn_0.Width = 22.5
        btn_0.GroupName = param
        btn_0.Enabled = False

        Set box = .Controls.Add("forms.checkbox.1", "chk_box_" & param, True)
        box.Caption = param
        box.Height = 45
        'box.Top = box.Top - 5
        box.Left = box.Left + 40
        box.Width = 60
        box.GroupName = param
        box.Value = True

        j = j + 1
        End With
    End If
Next

Me.Height = Label2.Height + 70 + (k + 1) * (55)
MultiPage1.Height = Me.Height

Me.Width = (fenetre.Width + 20) * 3
MultiPage1.Width = Me.Width

cmd_btn_ok.Top = Me.Height - 80
cmd_btn_ok.Width = 70
cmd_btn_ok.Left = Me.Width / 2 - 70
cmd_btn_cancel.Top = Me.Height - 80
cmd_btn_cancel.Width = 70
cmd_btn_cancel.Left = Me.Width / 2



Dim ChkBoxParam As Classe1

Set myEventHandlers = New Collection

Dim c As Control
For Each c In Me.MultiPage1.page1.Controls
    If TypeName(c) = "CheckBox" Then
        Set ChkBoxParam = New Classe1

        Set ChkBoxParam.CheckBoxParam = c

        myEventHandlers.Add ChkBoxParam
    End If
Next c

尝试的解决方案列表:

VBA: Using WithEvents on UserForms

vba dynamically created checkboxes onclick events

1 个答案:

答案 0 :(得分:0)

您需要在创建时为动态创建的控件Name提供,否则无法引用它们。

查看以编程方式创建和使用ActiveX和Form控件的概述,作为我对此问题的回答的一部分:

来自链接页面的示例:

使用VBA添加/修改/删除ActiveX命令按钮:

Sub activexControl_add()
    'create ActiveX control
    Dim ws As Worksheet: Set ws = ActiveSheet
    With ws.OLEObjects.Add("Forms.CommandButton.1")
        .Left = 25
        .Top = 25
        .Width = 75
        .Height = 75
        .Name = "xCommandButton1" 'name control immediately (so we can find it later)
    End With
End Sub

Sub activexControl_modify()
    ' modify activeX control's properties
    Dim ws As Worksheet: Set ws = ActiveSheet
    With ws.OLEObjects("xCommandButton1").Object
        .Caption = "abcxyz"
        .BackColor = vbGreen
    End With
End Sub

Sub activexControl_delete()
    ' delete activeX control
    Dim ws As Worksheet: Set ws = ActiveSheet
    ws.OLEObjects("xCommandButton1").Delete
End Sub