2016-12-11 2 views
0

次のコードは正常にフォームの初期化中にコマンドボタンを作成します。CommandButtonのVBA名?

create button 
     Dim Obj As Object 
     Set Obj = UserForm1.Controls.Add("Forms.CommandButton.1", "commandbuttondone", True) 
     With Obj 
      .Caption = "filled in" 
      .Left = 550 
      .Height = 40 
      .Width = 35 
      .Top = 5 
     End With 

私は上記で作成したコマンドボタンの名前は思う:中ので、「commandbuttondone」、それがクリックされたとき、私はそれが何かをしたいです

Private Sub commandbuttondone_Click() 
'Private Sub commandbutton_Click() 
'Private Sub commandbutton1_Click() 
'Sub commandbuttondone_Click() 
'Sub commandbutton_Click() 
'Sub commandbutton1_Click() 


MsgBox (nr_of_zeros) 

For test1 = 1 To nr_of_zeros + 1 'create textboxes 
    Dim ctrl   As Control 
    Dim absorb_text  As String 

    ' stack suggestion: 
    ' loop through all control in user form 
    For Each ctrl In Me.Controls 
     ' check if control is type TextBox 
     If TypeName(ctrl) = "TextBox" Then 
      ' if control name is 1 (first created TextBox in your array) 
      If ctrl.name = "1" Then 
       absorb_text = ctrl.Text 

       'the message box is for debug only 
       MsgBox absorb_text 
      End If 
     End If 
    Next ctrl 
Next test1 

End Sub 

、何が起こるではなく、さらにmsgbox(nr_of_zeros):シートのコードは、私はサブを作成しました。私はこの問題で何を理解していないのですか?フォームがmsgboxをポップアップすることを許可していないか、または名前が間違っていますか?あなたがDim Cb As MSForms.CommandButtonで変数を定義することによって設定すると、実行時にCommandButtonを作成し、後でSet Cb = UserForm1.Controls.Add("Forms.CommandButton.1", "commandbuttondone", True)でそれを設定することができます

+0

「commandbuttondone」コード:あなただけのコマンドボタンの名前とメッセージボックスを表示するには、それをクリックして下のコードを使用したら、次に

フォームへのオブジェクト。私はイベントがあるとは思わない。 – MatthewD

+1

このhttp://stackoverflow.com/q/3014421/293078をご覧ください。 John Walkenbachはこれまでに優れたページを持っていましたが、今は見つけられません。 –

+1

@Maximilian brutus IIIあなたは 'CommandButton'と' TextBox'の配列に関するあなたのpreviusポストに与えた答えを追加するのは混乱しています。あなたは今何を達成しようとしていますか?実行時に作成された 'CommandButton'をクリックすると' MsgBox'がポップアップしますか? –

答えて

1

、これがセットアップされますあなたのUser_Form1に「commandbuttondone」という名前の新しいコマンドボタンを。私はあなたが単純に加算していると信じて

Option Explicit 

Private Sub commandbuttondone_Click() 

Dim CBctrl    As Control 
Dim absorb_text   As String 

' loop through all control in user form 
For Each CBctrl In Me.Controls 
    ' check if control is type Command button 
    If TypeName(CBctrl) = "CommandButton" Then 
     ' if control name is "commandbuttondone" 
     If CBctrl.Name = "commandbuttondone" Then 
      absorb_text = CBctrl.Name 

      'the message box is for debug only 
      MsgBox absorb_text 
     End If 
    End If 
Next CBctrl 

End Sub