2017-06-11 65 views
1

我在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)當複選框被選中,相應的TEXTBOX必須顯示1 2)當減號被按下時,相應的TEXTBOX必須減量 3)時,加號被按下時,相應的TEXTBOX必須遞增 4 )當「FinalizarInspeção」被按下時,收集的所有數據必須發送到Excel,填寫工作表。

我根本不知道如何將每個按鈕/複選框鏈接到相應的文本框,而無需爲每個按鈕創建子例程!我會有〜500個子程序....這是不可能手動管理的....

+0

哪裏像? –

+0

對不起... imgurl鏈接到圖像添加! –

+1

如果您要動態添加控件,則需要如下所示:http://www.siddharthrout.com/2011/08/05/vba-control-arrays/它向您展示瞭如何捕獲事件:只需要do是爲您的控件命名,以便可以在觸發代碼中標識相關控件組。 –

回答

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 

用戶表單代碼 - 我的表單被命名爲「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

+0

謝謝,蒂姆! 它的工作就像一個魅力,雖然我不得不在我的界面做一些修改... 我想我不能在一個多頁面內做到這一點,所以我創建了與按鈕鏈接的幀,並使一個幀可見時間,基於哪個按鈕被激活 –