我正在用下面的方式动态加载一个Useform。
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中获取项目。
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,而不是在它的右边。
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发布于 2017-01-11 02:26:56
我在给你一个答案,即使你不再需要它。其他人可能会去找它。
下面是在用户表单中创建2个复选框的示例代码:
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……
https://stackoverflow.com/questions/41551928
复制相似问题