去掉设计师这个词
试试这个(尝试和测试)
Set NewComboBox = TempForm.Controls.Add("Forms.ComboBox.1")
跟进
试试这个。(经过试验和测试)
Option Explicit
Sub Sample()
Dim TempForm As Object
Dim Ret
Set TempForm = ThisWorkbook.VBProject.VBComponents.Add(3)
Ret = addComboBox(TempForm, "CBox", 1, "MyCombo", "1;2;3;4")
VBA.UserForms.Add(TempForm.Name).Show
End Sub
Function addComboBox(ByRef TempForm As Object, ByVal controlType As String, _
ByVal pos As Integer, ByVal strCaption As String, ByVal strValues As String)
Dim NewComboBox As MSForms.ComboBox
Dim n As Long, nLines As Long, i As Long
Dim arr As Variant
Set NewComboBox = TempForm.designer.Controls.Add("Forms.ComboBox.1")
arr = Split(strValues, ";")
With NewComboBox
.Name = strCaption & "_" & controlType & "_" & pos
.Top = 20 + (12 * pos)
.Left = 10
.Width = 150
.Height = 12
End With
n = 2
With TempForm
nLines = .CodeModule.CountOfLines
.CodeModule.InsertLines nLines + 1, "Private Sub UserForm_Initialize()"
For i = 0 To UBound(arr)
.CodeModule.InsertLines nLines + n, " " & _
NewComboBox.Name & ".AddItem " & arr(i)
n = n + 1
Next i
.CodeModule.InsertLines nLines + n, "End Sub"
End With
End Function
截屏
更多跟进
感谢您的解决方案,如果我必须多次调用 addComboBox,即添加两个或多个组合框,则会多次创建 UserForm_Initialize 子,这又是个问题。– 维克拉姆
在这种情况下,您必须检查UserForm_Initialize
proc 是否存在,然后对其进行解析。请参阅下面的代码。我S
为您的函数添加了一个新的可选参数。我正在使用它来将组合放在另一个下方。
Option Explicit
Sub Sample()
Dim TempForm As Object
Dim Ret
Set TempForm = ThisWorkbook.VBProject.VBComponents.Add(3)
Ret = addComboBox(TempForm, "CBox", 1, "MyCombo", "1;2;3;4")
Ret = addComboBox(TempForm, "CBox1", 1, "MyCombo1", "5;6;7;8", 20)
Ret = addComboBox(TempForm, "CBox2", 1, "MyCombo2", "9;10;11;12", 40)
VBA.UserForms.Add(TempForm.Name).Show
End Sub
Function addComboBox(ByRef TempForm As Object, ByVal controlType As String, _
ByVal pos As Integer, ByVal strCaption As String, ByVal strValues As String, _
Optional s As Long)
Dim NewComboBox As MSForms.ComboBox
Dim n As Long, nLines As Long, i As Long, uInitLine As Long
Dim arr As Variant
Dim MyModule As Object
Set NewComboBox = TempForm.Designer.Controls.Add("Forms.ComboBox.1")
arr = Split(strValues, ";")
With NewComboBox
.Name = strCaption & "_" & controlType & "_" & pos
.Top = 20 + (12 * pos) + s
.Left = 10
.Width = 150
.Height = 12
End With
'~~> Connect to the code module of the Userform
Set MyModule = ThisWorkbook.VBProject.VBComponents(TempForm.Name).CodeModule
'~~> Check if there is a procedure called UserForm_Initialize
On Error Resume Next
uInitLine = MyModule.ProcStartLine("UserForm_Initialize", 0)
On Error GoTo 0
With TempForm
'~~> UserForm_Initialize Found
If uInitLine > 0 Then
nLines = uInitLine + 2: n = 0
For i = 0 To UBound(arr)
.CodeModule.InsertLines nLines + n, " " & _
NewComboBox.Name & ".AddItem " & arr(i)
n = n + 1
Next i
Else
n = 2
nLines = .CodeModule.CountOfLines
.CodeModule.InsertLines nLines + 1, "Private Sub UserForm_Initialize()"
For i = 0 To UBound(arr)
.CodeModule.InsertLines nLines + n, " " & _
NewComboBox.Name & ".AddItem " & arr(i)
n = n + 1
Next i
.CodeModule.InsertLines nLines + n, "End Sub"
End If
End With
End Function
屏幕截图(用户表单)
屏幕截图(用户表单代码)