- 帖子
- 4901
- 主題
- 44
- 精華
- 24
- 積分
- 4916
- 點名
- 142
- 作業系統
- Windows 7
- 軟體版本
- Office 20xx
- 閱讀權限
- 150
- 性別
- 男
- 來自
- 台北
- 註冊時間
- 2010-4-30
- 最後登錄
- 2026-7-16
                
|
- Sub Auto_Open()
- Dim MyForm As VBComponent
- Set MyForm = ThisWorkbook.VBProject.VBComponents.Add(vbext_ct_MSForm)
- With MyForm
- .Properties("Caption") = "Test_Form"
- .Name = "Test_Form1"
- With .Designer
- With .Controls.Add("Forms.CommandButton.1")
- .Top = 50
- .Left = 100
- .Height = 20
- .Width = 20
- .Caption = ">>"
- .Name = "Move_Data"
- End With
- For i = 1 To 2
- With .Controls.Add("Forms.Listbox.1")
- .Top = 10
- .Left = (i - 1) * 100 + 20
- .Height = 120
- .Width = 80
- .Name = "MyList" & i
- End With
- Next
- End With
- With .CodeModule
- .InsertLines 1, "Private Sub UserForm_Initialize()"
- .InsertLines 2, "MyList1.List=array(1,2,3,4,5,6,7,8,9)"
- .InsertLines 3, "End Sub"
- .InsertLines 4, "Private Sub Move_Data_Click()"
- .InsertLines 5, "x = MyList1.ListIndex"
- .InsertLines 6, "MyList2.AddItem MyList1.List(x)"
- .InsertLines 7, "MyList1.RemoveItem x"
- .InsertLines 8, "End Sub"
- End With
- End With
- With ThisWorkbook.VBProject.VBComponents("ThisWorkbook").CodeModule
- .InsertLines 1, "Private Sub Workbook_BeforeClose(Cancel As Boolean)" & Chr(10) & _
- "With ThisWorkbook" & Chr(10) & _
- ".VBProject.VBComponents.Remove .VBProject.VBComponents(""Test_Form1"")" & Chr(10) & _
- "n = .VBProject.VBComponents(""ThisWorkbook"").CodeModule.CountOfLines" & Chr(10) & _
- ".VBProject.VBComponents(""ThisWorkbook"").CodeModule.DeleteLines 1, n" & Chr(10) & _
- ".Save" & Chr(10) & _
- "End With" & Chr(10) & _
- "End Sub"
- End With
- 'OpenForm '開啟檔案自動開啟表單
- End Sub
- Sub OpenForm()
- 'Test_Form1.Show 0 '開啟表單
- End Sub
複製代碼 回復 5# C.F
是這樣的效果嗎?
動態新增表單.zip (13.91 KB)
一般模組 |
|