【问题标题】:I have issue with create a dynamic created optionbuttons (radiobuttons), in Excel VBA Forms我在 Excel VBA 表单中创建动态创建的选项按钮(单选按钮)时遇到问题
【发布时间】:2021-05-17 20:23:50
【问题描述】:

我尝试了很多解决方案,但都没有成功,现在需要您的帮助。为了便于理解,我已经缩减了完整的代码。

我有一个带有树形固定选项按钮的表单,属于 Group1。将 OptionButton2 命名为 Optionbutton4。我想在 Group1 中创建一个名为 OptionButton1 的动态创建选项按钮。然后选择其中一个按钮,值应写入 i 个单元格。 OptionButton2 到 OptionButton4 工作正常,但不是代码创建的按钮。有超级大脑的人,想帮助我吗? (在最终代码中,我将创建一个循环来创建很多按钮)

Option Explicit
Dim opSel As String

Private Sub UserForm_initialize()
Dim OptionButton1 As Control
Set OptionButton1 = Controls.Add("Forms.OptionButton.1", "OptionButton1", True)
With OptionButton1
.Name = "OptionButton1"
.Caption = "OptionButton1"
.GroupName = "Group1"
.Value = 0
.Top = 5
.Left = 10
End With
End Sub

Private Sub OptionButton1_Click()
  opSel = OptionButton1.GroupName & " " & OptionButton1.Name
    Debug.Print "Selected button: " & opSel
    ThisWorkbook.Sheets("Blad1").Cells(1, 1).Value = "TEST-Button1"
End Sub

Private Sub OptionButton2_Click()
  opSel = OptionButton2.GroupName & " " & OptionButton2.Name
    Debug.Print "Selected button: " & opSel
    ThisWorkbook.Sheets("Blad1").Cells(2, 1).Value = "TEST-Button2"
End Sub

Private Sub OptionButton3_Click()
  opSel = OptionButton3.GroupName & " " & OptionButton3.Name
    Debug.Print "Selected button: " & opSel
    ThisWorkbook.Sheets("Blad1").Cells(3, 1).Value = "TEST-Button3"
End Sub

Private Sub OptionButton4_Click()
  opSel = OptionButton4.GroupName & " " & OptionButton4.Name
    Debug.Print "Selected button: " & opSel
    ThisWorkbook.Sheets("Blad1").Cells(4, 1).Value = "TEST-Button4"
End Sub


'Private Sub CommandButton1_Click()
'ThisWorkbook.Sheets("Blad1").Columns("A:A").Clear
'End Sub

【问题讨论】:

  • 动态添加控件时,最灵活的方法是使用“控件数组”——这样您就不必为添加的每个控件编写处理程序。例如:siddharthrout.com/index.php/2018/01/15/vba-control-arrays
  • Thanx @Tim,你的回答给了我一个提示,现在它就像发光一样工作。

标签: excel vba forms radio-button


【解决方案1】:

我在 1000 宽度的用户窗体中使用的解决方案:我使用 TAG 来捕获我稍后想在 Class1 模块中使用的值。编辑在 class1 模块中发生的事情。在我的最终解决方案中,我将在特定的 Excel 单元格中捕获并输入值。

'In class1 module:
Option Explicit
Public WithEvents CmdEvents As MSForms.OptionButton
Private Sub CmdEvents_Click()
  Debug.Print Me.CmdEvents.Tag
End Sub

'In form code
Dim cmdArray_1_2() As New Class1
Dim cmdArray_2_1() As New Class1
Dim cmdArray_2_2() As New Class1
Dim cmdArray_3_1() As New Class1
Dim cmdArray_3_2() As New Class1

Dim rng As Range
Dim rcell As Range
Dim Offset
Dim CountRowLoop
Dim topval As String
Dim starttop
Dim offsetcell As String

starttop = 135
topval = 165
offsetcell = 135
CountRowLoop = 1

Set rng = ThisWorkbook.Sheets("Sheet1").Range("A1:A" & ThisWorkbook.Sheets("Sheet1").UsedRange.Rows.Count)
For Each rcell In rng.Cells

'--OptionButtonSection1----------------
Dim optArtStat1 As Control, optArtStat2 As Control
Set optArtStat1 = Controls.Add("Forms.OptionButton.1", "optArtStat1_" & CountRowLoop, True)
With optArtStat1
    .Name = "optArtStat1_" & CountRowLoop
    .GroupName = "optArtStat" & CountRowLoop
    .Caption = "Option 1_" & CountRowLoop
    .Tag = "TESTING Option:1 Button:1 Row:" & CountRowLoop
    .Left = 610
        If CountRowLoop = 1 Then
            .Top = starttop
        End If
            If CountRowLoop > 1 Then
            .Top = offsetcell
        End If
    End With
ReDim Preserve cmdArray_1_1(1 To CountRowLoop)
Set cmdArray_1_1(CountRowLoop).CmdEvents = optArtStat1
'--OptionButtonSection2----------------
Set optArtStat2 = Controls.Add("Forms.OptionButton.1", "optArtStat2_" & CountRowLoop, True)
With optArtStat2
    .Name = "optArtStat2_" & CountRowLoop
    .GroupName = "optArtStat" & CountRowLoop
    .Caption = "Option 1_2_" & CountRowLoop
    .Tag = "TESTING Option:1 Button:2 Row:" & CountRowLoop
    .Left = 610
        If CountRowLoop = 1 Then
            .Top = starttop + 15
        End If
            If CountRowLoop > 1 Then
            .Top = offsetcell + 15
        End If
    End With
ReDim Preserve cmdArray_1_2(1 To CountRowLoop)
Set cmdArray_1_2(CountRowLoop).CmdEvents = optArtStat2

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-09
    • 1970-01-01
    • 1970-01-01
    • 2010-09-13
    • 1970-01-01
    • 1970-01-01
    • 2011-01-01
    • 2011-07-28
    相关资源
    最近更新 更多