Option Explicit
Sub MakeUserForm()
Dim MyUserForm As VBComponent
Dim NewOptionButton As Msforms.OptionButton
Dim NewCommandButton1 As Msforms.CommandButton
Dim NewCommandButton2 As Msforms.CommandButton
Dim MyComboBox As Msforms.ComboBox
Dim N, X As Integer, MaxWidth As Long
For N = 1 To ActiveWorkbook.VBProject.VBComponents.Count
If ActiveWorkbook.VBProject.VBComponents(N).Name = "NewForm" Then
ShowForm
Exit Sub
Else
End If
Next N
Set MyUserForm = ActiveWorkbook.VBProject _
.VBComponents.Add(vbext_ct_MSForm)
With MyUserForm
.Properties("Height") = 100
.Properties("Width") = 200
On Error Resume Next
.Name = "NewForm"
.Properties("Caption") = "Here is your user form"
End With
Set NewCommandButton1 = MyUserForm.Designer.Controls.Add("forms.CommandButton.1")
With NewCommandButton1
.Caption = "Cancel"
.Height = 18
.Width = 44
.Left = MaxWidth + 147
.Top = 6
End With
Set NewCommandButton2 = MyUserForm.Designer.Controls.Add("forms.CommandButton.1")
With NewCommandButton2
.Caption = "OK"
.Height = 18
.Width = 44
.Left = MaxWidth + 147
.Top = 28
End With
With MyUserForm.CodeModule
X = .CountOfLines
.InsertLines X + 1, "Sub CommandButton1_Click()"
.InsertLines X + 2, " Unload Me"
.InsertLines X + 3, "End Sub"
.InsertLines X + 4, ""
.InsertLines X + 5, "Sub CommandButton2_Click()"
.InsertLines X + 6, " Unload Me"
.InsertLines X + 7, "End Sub"
End With
Set MyComboBox = MyUserForm.Designer.Controls.Add("Forms.ComboBox.1")
With MyComboBox
.Name = "Combo1"
.Left = 10
.Top = 10
.Height = 16
.Width = 100
End With
ShowForm
End Sub
Sub ShowForm()
NewForm.Show
End Sub
|