返回列表 上一主題 發帖

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

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

我以下列程式碼 新增一個 UserForm 、CommandButton 再用一個 Class 來執行 Button Click 的動作,但 似乎 一直無法 照著 Class 內的動作去執行 ~~
我不知是哪個部份出了問題 ~~

   
   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 = "新增"
        .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 的內容


Option Explicit

Public WithEvents CmdBtn As MSForms.CommandButton
Private Sub CmdBtn_Click()
                MsgBox "Test O.K ! "
End Sub

其實我還有兩個 ListBox,我主要是點了Button 後 要將左邊 ListBox 的文字 帶到右邊去 ~~
上方是我先測試一下,結果就不成功了 ~~

TOP

是要搬移的

TOP

感謝您 ~~ 這的確是我要的~~~
只是因為我還會加其它功能 ~~ 所以我相將 按鈕的程式碼部份放到 class 中
請問可以怎麼做?

TOP

非常感謝您的指導 ~~ 但我遇到一個問題,我之前就遇到 了 ~~

執行這行的時候有時會無法執行 ~~ 不知為何 無法會如 此~
.Name = "Test_Form1"

TOP

會出現 error 75 訊息 ~~

TOP

我已經手動將表單移除,仍然是如此 ~~ 所以我 不知該如何作 ~~

TOP

記憶體啊 ~~~ 應該是如您所說 ~~ 因為 這問題不是每次發生 ~~
但有時是連續發生,有時 是 連續執行也不會發生 ~~

另外就是想請較 ~~ 如果我要取得 MyList2 中所得到的項目,我在Sub Auto_Open() 中
該如何取得?

我用 MyForm.Designer.Controls("MyList2") 不能成功 ~~

TOP

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      
        我新增了一個 Button 取得了 ListBox 的值,但我要如何讓userform 關閉 且讓
         原來的 Sub Auto_Open() 繼續往下跑~?

TOP

不好意思,後來試了 Unload Test_Form3 就可以關了  ~~
可是我要如何 返回原來的 Sub Auto_Open() 時,我如何取得 Item_GetList �堛漕漕И等堸琚H

TOP

        靜思自在 : 人的心地是一畦田,土地沒有播下好種子,也長不出好的果實。 -
返回列表 上一主題