如何在Userform VBA中使用dynamicbutton

我真的可以使用一些帮助。 我已经阅读了大约60多个网站,它不是单击(双关意图),或者是对我的应用程序不正确。 这里是简介:

目标:使用在“用户窗体”中dynamic创build的“提交”button将标题从选项button复制到工作表上的dynamic单元格,然后清除/closures用户窗体。

背景:从工作表中的列中的更改调用用户窗体。 以下是用于调用用户表单的代码片段:

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 

用户窗体显示后,它初始化。 它从电子表格中的列表中拉出,以填充有多less个选项button,它们的标题以及用户窗体上每个项目的尺寸。 代码是这样的:

 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 

问题:那么,如何获取在Userform中创build的btn,以将选定的OptionButton标题复制到dynamic单元格,然后清除/closuresUserform?

我知道这是一个延伸,但我试图填写从“目标”单元格触发用户窗体打开两列的单元格。 该代码填充当前的date/时间在Worksheet_Change剪切.Offset(0,1)中,但有没有方法将OptionButton标题放置在单元格.Offset(0,2)?

我对VBA来说还是很新的,这一点真的让我非常兴奋。

我会非常感激这方面的帮助。

谢谢! 乔

改变你的btnvariables为一个类的variables,并使用WithEvents将允许你访问dynamicbutton事件。

 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 

在这里输入图像说明