2017-08-02 136 views
1

我真的可以在這方面使用一些幫助。我已經閱讀了大約60多個網站,它不是點擊(雙關語意),或者它對我的應用程序不正確。以下是簡要說明:如何在Userform VBA中使用動態按鈕

目標:使用在用戶窗體中動態創建的「提交」按鈕將標題從選項按鈕複製到工作表上的動態單元格,然後清除/關閉用戶窗體。

背景:用戶窗體從工作表中的列中的更改被調用。 這裏是用來調用用戶窗體的代碼片段:

Private Sub Worksheet_Change(ByVal Target As Excel.Range) 
Dim lastRow As Long 

    With Worksheets("Test") 
     lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row 
    End With 
    With Target 
     If .Count > 1 Then Exit Sub 
     If Not Intersect(Range("B1:B" & lastRow), .Cells) Is Nothing Then 
      Application.EnableEvents = False 
      If IsEmpty(.Value) Then 
       .Offset(0, 1).ClearContents 
      Else 
       With .Offset(0, 1) 
        .NumberFormat = "mmm dd yyyy hh:mm:ss" 
        .Value = Now 
        UserForm1.Show 
       End With 
      End If 
      Application.EnableEvents = True 
     End If 
    End With 

End Sub 

用戶窗體顯示後,它初始化。它從電子表格中的列表中拉出來填充有多少個選項按鈕,它們的標題以及用戶窗體上每個項目的尺寸。該代碼是這樣的:

Sub UserForm_Initialize() 

Dim HLastRow As Integer 
Dim NoOfExplanations As Integer 
Dim TopPixels As Integer 
Dim UserFormHeight As Integer 
Dim UserFormWidth As Integer 
Dim Opt As Variant 
Dim i As Integer 
Dim ExplanationRow As Integer 
Dim lbl As MSForms.Label 
Dim LabelCap As String 
Dim btn As CommandButton 
Dim OtherInput As MSForms.TextBox 
Dim Margins As Integer 

    With Worksheets("Test") 
     HLastRow = .Cells(.Rows.Count, "H").End(xlUp).Row 
    End With 

NoOfExplanations = Application.WorksheetFunction.CountA(Worksheets("Test").Range("H2:H" & HLastRow)) 
Margins = 20 

LabelCap = "You have chosen a non sequential row for your team/subteam. Please select an explanation below before you are able to proceed" 
UserFormWidth = Len(LabelCap) * 2 
TopPixels = (18 * 2) 
UserFormHeight = TopPixels + 80 + (20 * NoOfExplanations) 

    With UserForm1 
     .Width = UserFormWidth + 40 
     .Height = UserFormHeight 
    End With 

    Set lbl = UserForm1.Controls.Add("Forms.Label.1") 
    With lbl 
     .Top = 10 
     .Left = 20 
     .Height = 20 
     .Width = UserFormWidth - 20 
     .Caption = LabelCap 
    End With 

ExplanationRow = 2 
For i = 1 To NoOfExplanations 

    Set Opt = UserForm1.Controls.Add("Forms.OptionButton.1", "OptionButton" & i, True) 

    Opt.Caption = Worksheets("Test").Cells(ExplanationRow, 8).Value 

    If Worksheets("Test").Cells(ExplanationRow, 8).Value = "Other" Then 
     Set OtherInput = UserForm1.Controls.Add("Forms.TextBox.1") 
     With OtherInput 
      .Top = TopPixels 
      .Width = UserFormWidth - (Len(Worksheets("Test").Cells(ExplanationRow, 8).Value) * 11) 
      .Left = UserFormWidth - (UserFormWidth - (Len(Worksheets("Test").Cells(ExplanationRow, 8).Value) * 11)) 
      .Height = 18 
     End With 
    End If 

    If Len(Worksheets("Test").Cells(ExplanationRow, 8).Value) > 45 Then 
     Opt.Width = UserFormWidth - 10 
     Opt.Height = 36 
     Opt.Left = 18 

     Opt.Top = TopPixels 
     TopPixels = TopPixels + 38 
    End If 

    If Len(Worksheets("Test").Cells(ExplanationRow, 8).Value) <= 45 Then 
     Opt.Width = UserFormWidth - 10 
     Opt.Height = 18 
     Opt.Left = 18 

     Opt.Top = TopPixels 
     TopPixels = TopPixels + 20 
    End If 
    ExplanationRow = ExplanationRow + 1 
Next i 

    Set btn = UserForm1.Controls.Add("Forms.CommandButton.1") 
    With btn 
     .Top = TopPixels 
     .Width = 40 
     .Left = ((UserFormWidth + 40)/2) - 20 
     .Height = 20 
     .Caption = "Submit" 
     .Name = btn 
    End With 
End Sub 

問:那麼,我如何才能在這裏創建了用戶窗體到兩個BTN複製所選選項按鈕標題的動態單元格,然後清除/關閉UserForm?

我知道這是一個拉伸,但我試圖從觸發Userform打開的「目標」單元格填寫兩列以上的單元格。代碼在Worksheet_Change中的.Offset(0,1)中填充當前日期/時間,但是有沒有辦法將OptionButton標題放在單元格中的.Offset(0,2)?

我對VBA還是很新的,這一件事情真的讓我非常興奮。

我會非常感激這方面的幫助。

謝謝! Joe

回答

1

將您的btn變量更改爲類級變量,並使用WithEvents將允許您訪問動態按鈕事件。

Private WithEvents btn As CommandButton 

Private Sub btn_Click() 
    Dim ctrl As Control 
    For Each ctrl In Me.Controls 
     If TypeName(ctrl) = "OptionButton" Then 
      If ctrl.Object.Value Then 
       MsgBox ctrl.Object.Caption 
      End If 
     End If 
    Next 
End Sub 

enter image description here

+0

非常感謝托馬斯!對不起,如果這是一個愚蠢的問題,但如何將'btn'更改爲類級變量?我完全按照GIF中的代碼設置了代碼,但它仍然無法正常工作,所以我認爲我錯過了該片段。 –

+0

類變量聲明在任何子例程之外,最好是模塊的頂部。確保你的子程序中沒有聲明同名的變量。 – 2017-08-02 20:54:22

+0

好的,真棒。我得到了它的工作。這是向正確方向邁出的重要一步。非常感謝托馬斯的幫助! –