我试图根据正在查看的特定工作表上的列数在 Excel 用户窗体中创建可变数量的控件(组合框)。理想情况下,我想在运行时删除现有的并创建新的,而不是创建 100 个左右,然后在可见和不可见之间来回切换。我现在所拥有的将创建一个组合框和循环,但它只创建 1。看起来组合框正在被覆盖,并以创建的最后一个组合框结束。有什么建议可以让所有内容在运行时都进入同一个用户窗体吗?
Private Sub CommandButton1_Click()
Dim cCont As New Control
Dim ws As Worksheet
Dim lc As Long
Dim i As Long
Set ws = Loan_Data
lc = Loan_Data.Cells(1, Columns.Count).End(xlToLeft).Column
For i = 1 To lc
Set cCont = Me.Controls.Add("Forms.CommandButton.1", "NewCombo" & i)
With cCont
.Caption = cCont.Name
.AutoSize = True
.Visible = True
End With
Next i
End Sub
我可以与您分享一个我用来在运行时创建一些组合框的过程示例。
Private Sub Agrega_Combo(Unidades As Integer)
'Procedimiento para agregar los ComboBox, etiquetas y unidades a la lista.
Dim i, j As Integer
Dim Cmb As Control
Dim Lbl As Control
'Ciclo para crear los ComboBox y Etiquetas en el 'ArrUnidades'
For i = 1 To UBound(ArrUnidades)
'Agrega el ComboBox
Set Cmb = Me.Controls.Add("Forms.combobox.1")
'Se establece el nombre y la posición del nuevo ComboBox
With Cmb
.Name = "Combobox" & i
.Left = 66
.Width = 36
If i = 1 Then
.Top = 34
Else
.Top = 34 + (24 * (i - 1))
End If
End With
'Agrega la Etiqueta'
Set Lbl = Me.Controls.Add("Forms.label.1")
With Lbl
.Name = "Label" & i
.Caption = ArrUnidades(i) & " :"
.Left = 30
.Width = 36
If i = 1 Then
.Top = 38
Else
.Top = 38 + (24 * (i - 1))
End If
End With
'Ciclo para agregar las unidades indicadas al llamar el procedimiento.
For j = 1 To Unidades
Me.Controls("ComboBox" & i).AddItem j
Next j
'Selecciona el primer valor de la lista.
Me.Controls("ComboBox" & i).Text = Me.Controls("ComboBox" & i).List(0)
Next i
End Sub
希望能帮助到你。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)