Board logo

¼ÐÃD: [µo°Ý] °ÊºA·s¼W UserForm ¤Î CommandButton «á ¦p¦ó¼gclickªº°Ê§@¡H [¥´¦L¥»­¶]

§@ªÌ: C.F    ®É¶¡: 2013-1-22 16:00     ¼ÐÃD: °ÊºA·s¼W UserForm ¤Î CommandButton «á ¦p¦ó¼gclickªº°Ê§@¡H

§Ú¥H¤U¦Cµ{¦¡½X ·s¼W¤@­Ó UserForm ¡BCommandButton ¦A¥Î¤@­Ó Class ¨Ó°õ¦æ Button Click ªº°Ê§@¡A¦ý ¦ü¥G ¤@ª½µLªk ·ÓµÛ Class ¤ºªº°Ê§@¥h°õ¦æ ~~
§Ú¤£ª¾¬O­þ­Ó³¡¥÷¥X¤F°ÝÃD ~~

   
   Dim MyNewForm As Object
    Dim NewListBox1 As MSForms.ListBox
    Dim NewLabel1 As MSForms.Label
    Dim NewCommandButton1 As MSForms.CommandButton
   
   
    Dim Btn1 As New Class1
    Dim MyAddItemCode$
    Dim UF_Name$
   
'   Create the UserForm
    Set MyNewForm = ThisWorkbook.VBProject.VBComponents.Add(vbext_ct_MSForm)
   
    With MyNewForm
         .Properties("Caption") = "Test_Form1"
         .Properties("Width") = 540
         .Properties("Height") = 350
         '.Name = "Test_Form1"
    End With


      Set NewCommandButton1 = MyNewForm.Designer.Controls _
      .Add("forms.CommandButton.1")
      
    With NewCommandButton1
   
        .Name = "Add_ItemBtn"
        .Left = 235
        .Top = 36
        .Height = 22
        .Width = 50
        .Caption = "·s¼W"
        .Font.Name = "¼Ð·¢Åé"
        .Font.Size = 12
        
    End With
        
  
    Set Btn1.CmdBtn = NewCommandButton1

      '   Show the form
    VBA.UserForms.Add(UF_Name$).Show 'vbModeless

    'Delete the form
    ThisWorkbook.VBProject.VBComponents.Remove VBComponent:=MyNewForm

    'ThisWorkbook.VBProject.VBComponents.Remove VBComponent:=Test1
    Set MyNewForm = Nothing


Class ªº¤º®e


Option Explicit

Public WithEvents CmdBtn As MSForms.CommandButton
Private Sub CmdBtn_Click()
                MsgBox "Test O.K ! "
End Sub
§@ªÌ: mark15jill    ®É¶¡: 2013-1-22 16:17

¦^´_ 1# C.F


    §A­nªº¥Øªº¬O¤°»ò?
     ªÅ¥Õªí³æ ²£¥X CommandButton?
     µM«á ÂICommandButton ­n¦³¤°»ò°Ê§@?
§@ªÌ: C.F    ®É¶¡: 2013-1-22 16:24

¨ä¹ê§ÚÁÙ¦³¨â­Ó ListBox¡A§Ú¥D­n¬OÂI¤FButton «á ­n±N¥ªÃä ListBox ªº¤å¦r ±a¨ì¥kÃä¥h ~~
¤W¤è¬O§Ú¥ý´ú¸Õ¤@¤U¡Aµ²ªG´N¤£¦¨¥\¤F ~~
§@ªÌ: mark15jill    ®É¶¡: 2013-1-22 16:33

¦^´_ 3# C.F


ÂI¤U¥h ¥ªÃä¨ì¥kÃä ¬O½Æ»sÁÙ¬O³æ¯Â·h²¾?
§@ªÌ: C.F    ®É¶¡: 2013-1-22 16:41

¬O­n·h²¾ªº
§@ªÌ: Hsieh    ®É¶¡: 2013-1-23 10:30

  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
½Æ»s¥N½X
¦^´_ 5# C.F


    ¬O³o¼Ëªº®ÄªG¶Ü?
[attach]14049[/attach]
[attach]14050[/attach]
¤@¯ë¼Ò²Õ
§@ªÌ: C.F    ®É¶¡: 2013-1-23 11:07

·PÁ±z ~~ ³oªº½T¬O§Ú­nªº~~~
¥u¬O¦]¬°§ÚÁÙ·|¥[¨ä¥¦¥\¯à ~~ ©Ò¥H§Ú¬Û±N «ö¶sªºµ{¦¡½X³¡¥÷©ñ¨ì class ¤¤
½Ð°Ý¥i¥H«ç»ò°µ¡H
§@ªÌ: Hsieh    ®É¶¡: 2013-1-23 21:52

¦^´_ 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
½Æ»s¥N½X
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
½Æ»s¥N½X
[attach]14058[/attach]
§@ªÌ: C.F    ®É¶¡: 2013-1-24 10:33

«D±`·PÁ±zªº«ü¾É ~~ ¦ý§Ú¹J¨ì¤@­Ó°ÝÃD¡A§Ú¤§«e´N¹J¨ì ¤F ~~

°õ¦æ³o¦æªº®É­Ô¦³®É·|µLªk°õ¦æ ~~ ¤£ª¾¬°¦ó µLªk·|¦p ¦¹~
.Name = "Test_Form1"
§@ªÌ: C.F    ®É¶¡: 2013-1-24 12:02

·|¥X²{ error 75 °T®§ ~~
§@ªÌ: Hsieh    ®É¶¡: 2013-1-24 15:30

  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 'Àˬdªí³æ¼Ò²Õ
  6.       If vbc.Name = "Test_Form1" Then Exit Sub '¦³¸Óªí³æ«h¸õ¥Xµ{§Ç
  7.    End If
  8. Next
  9. Set MyForm = ThisWorkbook.VBProject.VBComponents.Add(vbext_ct_MSForm) '·s¼Wªí³æ¼Ò²Õ
  10. With MyForm
  11.     .Properties("Caption") = "Test_Form" 'ªí³æ¼ÐÃD
  12.     .Name = "Test_Form1" 'ªí³æ¦WºÙ
  13.     With .Designer
  14.       With .Controls.Add("Forms.CommandButton.1") '·s¼W«ö¶s
  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") '²M³æ
  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 '¦bªí³æ¼Ò²Õ¤º·s¼Wµ{§Ç
  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"")" '±N«ö¶s¥[¤Jª«¥óÃþ§O
  36.       .InsertLines 4, "End Sub"
  37.     End With
  38. End With
  39. With ThisWorkbook.VBProject.VBComponents("ThisWorkbook").CodeModule '¼g¤JÃö³¬ÀÉ®×µ{§Çªºµ{¦¡½X
  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
½Æ»s¥N½X
¦^´_ 10# C.F


    À³¸Ó¬O§Aªºªí³æ¤w¸g¦s¦b¡A¦Ó§A¦b¤@¦¸°õ¦æ¸Óµ{§Ç¡A²£¥Í¦WºÙ½Ä¬ð
¸Õ¸Õ¬Ý
§@ªÌ: C.F    ®É¶¡: 2013-1-24 16:08

§Ú¤w¸g¤â°Ê±Nªí³æ²¾°£¡A¤´µM¬O¦p¦¹ ~~ ©Ò¥H§Ú ¤£ª¾¸Ó¦p¦ó§@ ~~
§@ªÌ: Hsieh    ®É¶¡: 2013-1-24 16:20

¦^´_ 12# C.F


   ¤â°Ê²¾°£ªí³æ«á¡A¦]°O¾ÐÅ餴¥¼ÄÀ©ñ¦¹ªí³æ
½Ð³s¦PThisworkbook¼Ò²Õ¤ºªº©Ò¦³µ{¦¡½X§R°£«á¦sÀÉ
§Y¥i°õ¦æAuto_Openµ{§Ç
§@ªÌ: C.F    ®É¶¡: 2013-1-24 16:45

°O¾ÐÅé°Ú ~~~ À³¸Ó¬O¦p±z©Ò»¡ ~~ ¦]¬° ³o°ÝÃD¤£¬O¨C¦¸µo¥Í ~~
¦ý¦³®É¬O³sÄòµo¥Í¡A¦³®É ¬O ³sÄò°õ¦æ¤]¤£·|µo¥Í ~~

¥t¥~´N¬O·Q½Ð¸û ~~ ¦pªG§Ú­n¨ú±o MyList2 ¤¤©Ò±o¨ìªº¶µ¥Ø¡A§Ú¦bSub Auto_Open() ¤¤
¸Ó¦p¦ó¨ú±o¡H

§Ú¥Î MyForm.Designer.Controls("MyList2") ¤£¯à¦¨¥\ ~~
§@ªÌ: C.F    ®É¶¡: 2013-1-25 14:11

Private Sub MyNewBtn2_Click()
        Dim i%
        Dim Get_Item1
   
        With Test_Form3
               
             MsgBox .Item_GetList.ListCount
            
             If .Item_GetList.ListCount <> 0 Then
                                 ReDim Get_Item1(.Item_GetList.ListCount - 1) As String
                 For i = 0 To .Item_GetList.ListCount - 1
                     Get_Item1(i) = .Item_GetList.List(i)
                     MsgBox Get_Item1(i)
                 Next i
             End If
            
            
        End With
End Sub      
        §Ú·s¼W¤F¤@­Ó Button ¨ú±o¤F ListBox ªº­È¡A¦ý§Ú­n¦p¦óÅýuserform Ãö³¬ ¥BÅý
         ­ì¨Óªº Sub Auto_Open() Ä~Äò©¹¤U¶]~¡H
§@ªÌ: C.F    ®É¶¡: 2013-1-25 14:20

¤£¦n·N«ä¡A«á¨Ó¸Õ¤F Unload Test_Form3 ´N¥i¥HÃö¤F  ~~
¥i¬O§Ú­n¦p¦ó ªð¦^­ì¨Óªº Sub Auto_Open() ®É¡A§Ú¦p¦ó¨ú±o Item_GetList ùتº¨º¨Ç¶µ¥Ø°Ú¡H
§@ªÌ: wanmas    ®É¶¡: 2013-2-22 16:25

¦^´_ 8# Hsieh


    ·Q½Ð°Ý ¦b·s¼WCommandButton ¤§«á

    ³oÃä¬O·s¼WButtonªº«ö¶s¨Æ¥ó¶Ü¡H
    With .CodeModule '¦bªí³æ¼Ò²Õ¤º·s¼Wµ{§Ç
      .InsertLines 1, "Private Sub UserForm_Initialize()"
      .InsertLines 2, "MyList1.List=array(1,2,3,4,5,6,7,8,9)"
      .InsertLines 3, "Set obj.MyBut = Controls(""Move_Data"")" '±N«ö¶s¥[¤Jª«¥óÃþ§O
      .InsertLines 4, "End Sub"
    End With

   ¨º°£¤F³o­Ó¤èªk¥~¯à¥Î¨ä¥Lªº¤èªk¥h·s¼W«ö¶sªº¨Æ¥ó¡H
§@ªÌ: Hsieh    ®É¶¡: 2013-2-22 16:34

¦^´_ 17# wanmas
¦]¬°¦¹°ÝÃD¬O±q·s¼Wªí³æ¶}©l
¦¹¬qµ{§Ç¬Oªí³æ¼Ò²Õªºªì©l¤Æµ{§Ç
¥Øªº¦b·íªí³æ¶}±Ò®É±Nªí³æ¤ºª«¥óªº³]©w¥[¤J
­Ó¤H¨Ã¤£«Øij¨Ï¥Î³o¼Ëªº¤èªk¾Þ§@ªí³æ
VBA¨Æª«¥ó¾É¦Vªºµ{¦¡»y¨¥
°£«D¯S§O¥Øªº¡A§_«hÀ³¥ý±Nª«¥ó³£³]¸m¦n
¨Ã¥B¨C­Óª«¥ó©Ò»Ýªºµ{§Ç¹w¥ý¼g¦n
©¡®É¥u»Ý±Ò°Êªí³æ§Y¥i°õ¦æ
³o¼Ë¦bµ{¦¡¼¶¼g¤¤¤ñ¸û®e©ö°»¿ù»P­×¥¿
§@ªÌ: modelcrazyer    ®É¶¡: 2016-4-5 22:37

§Úªº°ÝÃD¤ñ¸û²³æ¤@ÂI¡Aµ{¦¡ªº¥Øªº¬O¦b¬J¦³ªºªí³æ(UserForm3)¡A·s¼W3­ÓTextBox¡AµM«á¨C­ÓTextBox¥i¥H°õ¦æfunction(y_m_dinput (A)¡AÅܼƬ°A)¡Aµ{¦¡½X¦p¤U:
For X2 = 1 To 3
Set theTextBox = UserForm3("frame1").Controls.Add("Forms.TextBox.1", "TextBox" & TeCount + X2, True)
  With theTextBox
     .Name = "TextBox" & TeCount + X2
     If X2 = 1 Then
     .Left = 210
     ElseIf X2 = 2 Then
     .Left = 384
     Else
      .Left = 492
     End If
     .Width = 60
     .Top = K2 + 18
    myName = "Userform3"
    With ThisWorkbook.VBProject.VBComponents(myName).CodeModule
     '.DeleteLines 1, .countoflines
      Test = .countoflines - 44
     .insertlines Test, "Private Sub TextBox" & TeCount + X2 & "_Change()"
     .insertlines Test + 1, "A =" & TeCount + X2
     .insertlines Test + 2, "y_m_dinput (A)"
     .insertlines Test + 3, "End sub"
    End With
  End With
Next X2
µ{¦¡ªºµ²ªG¥i¥H¶]¥X3­ÓTextBox¡A¤]¥i¥H¼g¤F¥X¬Û¹ïÀ³ªºµ{§Ç¡A¦ý¬O´N¬OµLªk°õ¦æfunction.
½Ð°Ý°ÝÃD¥X¦b­þ??




Åwªï¥úÁ{ ³Â»¶®a±Ú°Q½×ª©ª© (http://forum.twbts.com/)