|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
Sub 建立窗体文件并运行() Dim TpForm As Object Application.VBE.MainWindow.Visible = False '防止窗口闪动 Set TpForm = ThisWorkbook.VBProject.VBComponents.Add(3) '建立窗体 With TpForm .Properties("Caption") = "新窗体" .Properties("Width") = 300 .Properties("Height") = 200 .Properties("Name") = "窗体文件" End With With TpForm.CodeModule '为窗体添加代码 .DeleteLines 1, .CountOfLines '以下双引号中的代码即为Initialize关联事件代码 .InsertLines 1, "Private Sub UserForm_Initialize()" .InsertLines 2, "Dim frm As Object" .InsertLines 3, "On Error Resume Next" .InsertLines 4, "Set frm = VBA.UserForms(""窗体文件"")" .InsertLines 5, "On Error GoTo 0" .InsertLines 6, "If Not frm Is Nothing Then" .InsertLines 7, "MsgBox ""我是原来的窗体哟""" .InsertLines 8, "Else" .InsertLines 9, "MsgBox ""我是新建的窗体""" .InsertLines 10, "End If" .InsertLines 11, "End Sub" End With VBA.UserForms.Add(TpForm.Name).Show '显示窗体 End Sub |
|