我是这个论坛的新人。我在VBA方面有一些经验,但我是UserForms的新手。这看起来很简单,但我只想关闭使用下面的代码创建的用户表单。我到处寻找答案,似乎没什么用。 "卸载我"或者"卸载Userform"没有效果。我只是想在单击按钮时关闭表单。我也尝试删除工作簿中的所有形状,但我仍然坚持使用该表单,所以任何帮助都将非常感激。这是我正在使用的代码:
Sub TypeForm()
Dim DocForm As Object
Dim NewFrame As MSForms.Frame
Dim NewButton As MSForms.CommandButton
Dim NewListBox As MSForms.ListBox
Dim x As Integer
Dim Line As Integer
Application.VBE.MainWindow.Visible = False
Set DocForm = ThisWorkbook.VBProject.VBComponents.Add(3)
With DocForm
.Properties("Caption") = "Document type"
.Properties("Width") = 300
.Properties("Height") = 270
End With
Set NewListBox = DocForm.Designer.Controls.Add("Forms.listbox.1")
With NewListBox
.Name = "lst_1"
.Top = 10
.Left = 10
.Width = 150
.Height = 230
.Font.Size = 8
.Font.Name = "Tahoma"
.BorderStyle = fmBorderStyleOpaque
.SpecialEffect = fmSpecialEffectSunken
End With
Set NewButton = DocForm.Designer.Controls.Add("Forms.commandbutton.1")
With NewButton
.Name = "cmd_1"
.Caption = "Accept"
.Accelerator = "M"
.Top = 10
.Left = 200
.Width = 80
.Height = 20
.Font.Size = 8
.Font.Name = "Tahoma"
.BackStyle = fmBackStyleOpaque
End With
lstBoxData = "Data 1,Data 2,Data 3,Data 4,Data 5,Data 6,Data 7,Data 8,Data 9,Data 10"
DocForm.CodeModule.InsertLines 1, "Private Sub UserForm_Initialize()"
DocForm.CodeModule.InsertLines 2, " me.lst_1.addItem ""Type 1"" "
DocForm.CodeModule.InsertLines 3, " me.lst_1.addItem ""Type 2"" "
DocForm.CodeModule.InsertLines 4, " me.lst_1.addItem ""Type 3"" "
DocForm.CodeModule.InsertLines 5, " me.lst_1.addItem ""Type 4"" "
DocForm.CodeModule.InsertLines 6, " me.lst_1.addItem ""Type 5"" "
DocForm.CodeModule.InsertLines 7, " me.lst_1.addItem ""Type 6"" "
DocForm.CodeModule.InsertLines 8, " me.lst_1.addItem ""Type 7"" "
DocForm.CodeModule.InsertLines 9, " me.lst_1.addItem ""Type 8"" "
DocForm.CodeModule.InsertLines 10, " me.lst_1.addItem ""Type 9"" "
DocForm.CodeModule.InsertLines 11, " me.lst_1.addItem ""Type 10"" "
DocForm.CodeModule.InsertLines 12, "End Sub"
DocForm.CodeModule.InsertLines 13, "Private Sub cmd_1_Click()"
DocForm.CodeModule.InsertLines 14, " If me.lst_1.text <>"""" Then"
DocForm.CodeModule.InsertLines 15, " Activecell.Offset(0, 2).value = me.lst_1.text"
DocForm.CodeModule.InsertLines 16, " Unload me"
DocForm.CodeModule.InsertLines 17, " Exit Sub"
DocForm.CodeModule.InsertLines 18, " End If"
DocForm.CodeModule.InsertLines 19, "End Sub"
VBA.UserForms.Add(DocForm.Name).Show
ThisWorkbook.VBProject.VBComponents.Remove VBComponent:=DocForm
End Sub
此代码位于自包含的.xlsm文件中,无论是否重要。提前谢谢!