按下按钮后重写动态文本框

时间:2017-06-11 02:02:04

标签: excel-vba checkbox dynamic vba excel

我在VBA中创建了一个代码,用于使用多页面控件收集数据。在每个页面中,我根据Excel中工作表中的行动态添加了复选框,对于每个复选框,都有一个文本框和2个命令按钮,如下图所示:

输入窗口: image

自动添加控件的代码是:

Private Sub UserForm_Initialize()
fmat_disp.Value = 0
fmat_set.Value = 0


'---------------------------------------------------------------------------------------------
'Inspeção de Mecânica
Sheets("Mecânica").Activate
n_anom = Application.WorksheetFunction.CountA(Range("1:1")) - 1
AreasInspecao.mecanica.ScrollHeight = 10 + 18 * (n_anom)
For i = 1 To n_anom
'Selecionar anomalia
Set SelAnom = AreasInspecao.mecanica.Controls.Add("Forms.CheckBox.1", "sel_anom_" & i)
SelAnom.Caption = Worksheets("Mecânica").Cells(1, i + 1)
SelAnom.AutoSize = True
SelAnom.Height = 18
SelAnom.Left = 5
SelAnom.Top = 5 + (SelAnom.Height) * (i - 1)
SelAnom.Tag = i

同样适用于文本框和加号/减号按钮,只更改字幕。

我想要的是: 1)当CHECKBOX被检查时,相应的TEXTBOX必须显示1 2)当MINUS标志被按下时,相应的TEXTBOX必须递减 3)当PLUS符号被按下时,相应的TEXTBOX必须递增 4)当“FinalizarInspeção”被按下时,所有收集的数据必须发送到Excel,填写工作表。

我根本不知道如何将每个按钮/复选框链接到相应的文本框而不为每个文本框创建子例程!我将有~500个子程序....这是不可能手动管理....

1 个答案:

答案 0 :(得分:0)

好的,这是处理复选框和按钮上的点击事件的粗略轮廓。

用于捕获点击的前两个自定义类:每个都非常简单 - 他们所做的就是在用户表单上调用一个方法,并将单击的控件作为参数。

'clsCheck
Public WithEvents chk As MSForms.CheckBox

Private Sub chk_Click()
    frmExample.HandleClick chk
End Sub

'clsButton
Public WithEvents btn As MSForms.CommandButton

Private Sub btn_Click()
    frmExample.HandleClick btn
End Sub

Userform代码 - 我的表单名为“frmExample”。

请注意命名约定,它允许将控件组视为“单元”。

Option Explicit

'These two global collections hold instances of the custom classes
Dim colCheckBoxes As Collection
Dim colButtons As Collection


Private Sub UserForm_Activate()

    Const CON_HT As Long = 18
    Dim x As Long, cbx As MSForms.CheckBox, t
    Dim btn As MSForms.CommandButton, txt As MSForms.TextBox
    Dim oCheck As clsCheck, oButton As clsButton

    Set colCheckBoxes = New Collection
    Set colButtons = New Collection

    For x = 1 To 10

        t = 5 + CON_HT * (x - 1)

        Set cbx = Me.Controls.Add("Forms.CheckBox.1", "cbox_" & x)
        cbx.Caption = "Checkbox" & x
        cbx.Width = 80
        cbx.Height = CON_HT
        cbx.Left = 5
        cbx.Top = t
        colCheckBoxes.Add GetCheckHandler(cbx) '<< save in collection

        Set btn = Me.Controls.Add("Forms.CommandButton.1", "btnplus_" & x)
        btn.Caption = "+"
        btn.Height = CON_HT
        btn.Width = 20
        btn.Left = 90
        btn.Top = t
        btn.Enabled = False '<<buttons start off disabled
        colButtons.Add GetButtonHandler(btn) '<< save in collection

        Set btn = Me.Controls.Add("Forms.CommandButton.1", "btnminus_" & x)
        btn.Caption = "-"
        btn.Height = CON_HT
        btn.Width = 20
        btn.Left = 130
        btn.Top = t
        btn.Enabled = False '<<buttons start off disabled
        colButtons.Add GetButtonHandler(btn) '<< save in collection

        'no events are captured for the textboxes...
        Set txt = Me.Controls.Add("Forms.Textbox.1", "txt_" & x)
        txt.Width = 30
        txt.Height = CON_HT
        txt.Left = 170
        txt.Top = t

    Next x

End Sub

'All "clicked" controls saved in instances of the custom classes
'   get passed here. Handle based on control type/name
Public Sub HandleClick(ctrl As MSForms.Control)
    Dim num
    num = Split(ctrl.Name, "_")(1) 'which set of controls are we working with?
    Dim txt As MSForms.TextBox
    'get the matching text box...
    Set txt = Me.Controls("txt_" & num)
    If ctrl.Name Like "cbox_*" Then
        If ctrl.Value Then txt.Value = 1
        Me.Controls("btnplus_" & num).Enabled = ctrl.Value
        Me.Controls("btnminus_" & num).Enabled = ctrl.Value
    ElseIf ctrl.Name Like "btnplus_*" Then
        txt.Value = txt.Value + 1
    ElseIf ctrl.Name Like "btnminus_*" Then
        txt.Value = txt.Value - 1
    End If
End Sub


'couple of "factory" functions for the event-handling classes
Private Function GetCheckHandler(cb As MSForms.CheckBox)
    Dim rv As New clsCheck
    Set rv.chk = cb
    Set GetCheckHandler = rv
End Function

Private Function GetButtonHandler(btn As MSForms.CommandButton)
    Dim rv As New clsButton
    Set rv.btn = btn
    Set GetButtonHandler = rv
End Function

示例文件:https://www.dropbox.com/s/k74c08m0zkwn9l7/tmpFormEvents.xlsm?dl=0