返回列表 上一主題 發帖

[發問] 動態新增 UserForm 及 CommandButton 後 如何寫click的動作?

  1. Sub Auto_Open()
  2. Dim MyForm As VBComponent
  3. Set MyForm = ThisWorkbook.VBProject.VBComponents.Add(vbext_ct_MSForm)
  4. With MyForm
  5.     .Properties("Caption") = "Test_Form"
  6.     .Name = "Test_Form1"
  7.     With .Designer
  8.       With .Controls.Add("Forms.CommandButton.1")
  9.           .Top = 50
  10.           .Left = 100
  11.           .Height = 20
  12.           .Width = 20
  13.           .Caption = ">>"
  14.           .Name = "Move_Data"
  15.       End With
  16.       For i = 1 To 2
  17.       With .Controls.Add("Forms.Listbox.1")
  18.           .Top = 10
  19.           .Left = (i - 1) * 100 + 20
  20.           .Height = 120
  21.           .Width = 80
  22.           .Name = "MyList" & i
  23.       End With
  24.       Next
  25.     End With
  26.     With .CodeModule
  27.       .InsertLines 1, "Private Sub UserForm_Initialize()"
  28.       .InsertLines 2, "MyList1.List=array(1,2,3,4,5,6,7,8,9)"
  29.       .InsertLines 3, "End Sub"
  30.       .InsertLines 4, "Private Sub Move_Data_Click()"
  31.       .InsertLines 5, "x = MyList1.ListIndex"
  32.       .InsertLines 6, "MyList2.AddItem  MyList1.List(x)"
  33.       .InsertLines 7, "MyList1.RemoveItem x"
  34.       .InsertLines 8, "End Sub"
  35.     End With
  36. End With
  37. With ThisWorkbook.VBProject.VBComponents("ThisWorkbook").CodeModule
  38.   .InsertLines 1, "Private Sub Workbook_BeforeClose(Cancel As Boolean)" & Chr(10) & _
  39. "With ThisWorkbook" & Chr(10) & _
  40. ".VBProject.VBComponents.Remove .VBProject.VBComponents(""Test_Form1"")" & Chr(10) & _
  41. "n = .VBProject.VBComponents(""ThisWorkbook"").CodeModule.CountOfLines" & Chr(10) & _
  42. ".VBProject.VBComponents(""ThisWorkbook"").CodeModule.DeleteLines 1, n" & Chr(10) & _
  43. ".Save" & Chr(10) & _
  44. "End With" & Chr(10) & _
  45. "End Sub"
  46. End With
  47. 'OpenForm '開啟檔案自動開啟表單
  48. End Sub
  49. Sub OpenForm()
  50. 'Test_Form1.Show 0 '開啟表單
  51. End Sub
複製代碼
回復 5# C.F


    是這樣的效果嗎?
play.gif
動態新增表單.zip (13.91 KB)
一般模組
學海無涯_不恥下問

TOP

回復 7# C.F
class1模組
  1. Public WithEvents MyBut As MSForms.CommandButton

  2. Private Sub MyBut_Click()
  3. With Test_Form1
  4.   x = .MyList1.ListIndex
  5.   If x = -1 Then Exit Sub
  6.   .MyList2.AddItem .MyList1.List(x)
  7.   .MyList1.RemoveItem x
  8. End With
  9. End Sub
複製代碼
Module1模組
  1. Public obj As New Class1
  2. Sub Auto_Open()
  3. Dim MyForm As VBComponent
  4. Set MyForm = ThisWorkbook.VBProject.VBComponents.Add(vbext_ct_MSForm)
  5. With MyForm
  6.     .Properties("Caption") = "Test_Form"
  7.     .Name = "Test_Form1"
  8.     With .Designer
  9.       With .Controls.Add("Forms.CommandButton.1")
  10.           .Top = 50
  11.           .Left = 100
  12.           .Height = 20
  13.           .Width = 20
  14.           .Caption = ">>"
  15.           .Name = "Move_Data"
  16.       End With
  17.       For i = 1 To 2
  18.       With .Controls.Add("Forms.Listbox.1")
  19.           .Top = 10
  20.           .Left = (i - 1) * 100 + 20
  21.           .Height = 120
  22.           .Width = 80
  23.           .Name = "MyList" & i
  24.       End With
  25.       Next
  26.     End With
  27.     With .CodeModule
  28.       .InsertLines 1, "Private Sub UserForm_Initialize()"
  29.       .InsertLines 2, "MyList1.List=array(1,2,3,4,5,6,7,8,9)"
  30.       .InsertLines 3, "Set obj.MyBut = Controls(""Move_Data"")"
  31.       .InsertLines 4, "End Sub"
  32.     End With
  33. End With
  34. With ThisWorkbook.VBProject.VBComponents("ThisWorkbook").CodeModule
  35.   .InsertLines 1, "Private Sub Workbook_BeforeClose(Cancel As Boolean)" & Chr(10) & _
  36. "With ThisWorkbook" & Chr(10) & _
  37. ".VBProject.VBComponents.Remove .VBProject.VBComponents(""Test_Form1"")" & Chr(10) & _
  38. "n = .VBProject.VBComponents(""ThisWorkbook"").CodeModule.CountOfLines" & Chr(10) & _
  39. ".VBProject.VBComponents(""ThisWorkbook"").CodeModule.DeleteLines 1, n" & Chr(10) & _
  40. ".Save" & Chr(10) & _
  41. "End With" & Chr(10) & _
  42. "End Sub"
  43. End With
  44. 'OpenForm '開啟檔案自動開啟表單
  45. End Sub
  46. Sub OpenForm()
  47. 'Test_Form1.Show 0 '開啟表單
  48. End Sub
複製代碼
動態新增表單.rar (16.51 KB)
學海無涯_不恥下問

TOP

  1. Public obj As New Class1
  2. Sub Auto_Open()
  3. Dim MyForm As VBComponent
  4. For Each vbc In ThisWorkbook.VBProject.VBComponents
  5.    If vbc.Type = 3 Then '檢查表單模組
  6.       If vbc.Name = "Test_Form1" Then Exit Sub '有該表單則跳出程序
  7.    End If
  8. Next
  9. Set MyForm = ThisWorkbook.VBProject.VBComponents.Add(vbext_ct_MSForm) '新增表單模組
  10. With MyForm
  11.     .Properties("Caption") = "Test_Form" '表單標題
  12.     .Name = "Test_Form1" '表單名稱
  13.     With .Designer
  14.       With .Controls.Add("Forms.CommandButton.1") '新增按鈕
  15.           .Top = 50
  16.           .Left = 100
  17.           .Height = 20
  18.           .Width = 20
  19.           .Caption = ">>"
  20.           .Name = "Move_Data"
  21.       End With
  22.       For i = 1 To 2
  23.       With .Controls.Add("Forms.Listbox.1") '清單
  24.           .Top = 10
  25.           .Left = (i - 1) * 100 + 20
  26.           .Height = 120
  27.           .Width = 80
  28.           .Name = "MyList" & i
  29.       End With
  30.       Next
  31.     End With
  32.     With .CodeModule '在表單模組內新增程序
  33.       .InsertLines 1, "Private Sub UserForm_Initialize()"
  34.       .InsertLines 2, "MyList1.List=array(1,2,3,4,5,6,7,8,9)"
  35.       .InsertLines 3, "Set obj.MyBut = Controls(""Move_Data"")" '將按鈕加入物件類別
  36.       .InsertLines 4, "End Sub"
  37.     End With
  38. End With
  39. With ThisWorkbook.VBProject.VBComponents("ThisWorkbook").CodeModule '寫入關閉檔案程序的程式碼
  40.   .InsertLines 1, "Private Sub Workbook_BeforeClose(Cancel As Boolean)" & Chr(10) & _
  41. "With ThisWorkbook" & Chr(10) & _
  42. ".VBProject.VBComponents.Remove .VBProject.VBComponents(""Test_Form1"")" & Chr(10) & _
  43. "n = .VBProject.VBComponents(""ThisWorkbook"").CodeModule.CountOfLines" & Chr(10) & _
  44. ".VBProject.VBComponents(""ThisWorkbook"").CodeModule.DeleteLines 1, n" & Chr(10) & _
  45. ".Save" & Chr(10) & _
  46. "End With" & Chr(10) & _
  47. "End Sub"
  48. End With
  49. 'OpenForm '開啟檔案自動開啟表單
  50. End Sub
  51. Sub OpenForm()
  52. 'Test_Form1.Show 0 '開啟表單
  53. End Sub
複製代碼
回復 10# C.F


    應該是你的表單已經存在,而你在一次執行該程序,產生名稱衝突
試試看
學海無涯_不恥下問

TOP

回復 12# C.F


   手動移除表單後,因記憶體仍未釋放此表單
請連同Thisworkbook模組內的所有程式碼刪除後存檔
即可執行Auto_Open程序
學海無涯_不恥下問

TOP

回復 17# wanmas
因為此問題是從新增表單開始
此段程序是表單模組的初始化程序
目的在當表單開啟時將表單內物件的設定加入
個人並不建議使用這樣的方法操作表單
VBA事物件導向的程式語言
除非特別目的,否則應先將物件都設置好
並且每個物件所需的程序預先寫好
屆時只需啟動表單即可執行
這樣在程式撰寫中比較容易偵錯與修正
學海無涯_不恥下問

TOP

        靜思自在 : 我們最大的敵人不是別人.可能是自己。
返回列表 上一主題