首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何向UserForm动态添加YES或NO CheckBoxes?

如何向UserForm动态添加YES或NO CheckBoxes?
EN

Stack Overflow用户
提问于 2017-01-10 00:00:17
回答 2查看 343关注 0票数 2

我正在用下面的方式动态加载一个Useform。

代码语言:javascript
运行
复制
Sub UserForm_Initialize()
    With Worksheets("SetupQuestions")
        Lrow = Worksheets("SetupQuestions").Cells(Rows.Count, 1).End(xlUp).Row
        Set rngSource = .Range("A2:B" & Lrow)
    End With
    NewrngSource = Replace(rngSource.Address, "$", "")
                With ListBox1
                    .Value = "None"
                    .ColumnHeads = True
                    .ColumnCount = 2
                    .ColumnWidths = "50;100"
                    .RowSource = "SetupQuestions!" & NewrngSource & ""
                    .MultiSelect = fmMultiSelectMulti
                    .BoundColumn = 1
                End With
End Sub

我正在尝试找出一种方法来添加与ListBox中的项相对应的CheckBoxes。我可以很容易地从我的ListBox中获取项目。

代码语言:javascript
运行
复制
Sub CommandButton1_Click()
    Dim text As String
    Dim i As Integer
    For i = 0 To Me.ListBox1.ListCount - 1
        If Me.ListBox1.Selected(i) Then
            text = text & Me.ListBox1.List(i, 0) & ". " & Me.ListBox1.List(i, 1) & " " & Chr(10)
        End If
    Next i
    Sheets("NEW Format").Range("BB1").Value = text
    Unload Me
End Sub

我就是想不出如何动态添加CheckBoxes。这是我的Lisbox的一个视图,以及一个用于YES/NO对象的CheckBox,但我在这里只列出了一个,并且我真的想要与列表中的每一项相对应的所有CheckBoxes。

我在网上看到了一个看起来很有前途的示例脚本,但它在我的ListBox下面添加了CheckBoxes,而不是在它的右边。

代码语言:javascript
运行
复制
For Each rngCell In rngSource
    If rngCell.Value <> "" Then
        Set NewChkBx = Me.Controls.Add("Forms.CheckBox.1")
        With NewChkBx
            .Caption = rngCell.Value
            .Left = 5
            .Top = TopPos
            .AutoSize = True
            If .Width > MaxWidth Then MaxWidth = .Width
        End With
        TopPos = TopPos + 15
    End If
Next rngCell
EN

Stack Overflow用户

发布于 2017-01-11 02:26:56

我在给你一个答案,即使你不再需要它。其他人可能会去找它。

下面是在用户表单中创建2个复选框的示例代码:

代码语言:javascript
运行
复制
Option Explicit 'inside userform's code

Private Sub AddCheckboxes()
Dim i&
For i=1 to 2
    with Me.controls.add("Forms.Checkbox.1","CheckBx" & i ,true) '3 arguments : the first is a fix one (do not mind the ".1" inside it, it is the way it needs to be ; the 2nd is the name of the control, the 3rd means visible=true
        .top=50 + (i-1)*20 'commencing at 50 and having a distance of 20 between controls
        .Left = 50
        .Caption= "Autorize or Do something as #" & i 'for example
    end with
next i
end sub

如果有许多控件,最好的方法是将它们放在一个字典数组中,在我的例子中,我经常使用类的字典(类中有许多控件),这样我就可以为所有这些控件创建一个公共行为,例如_mousemove_mousedown……

票数 0
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41551928

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档