2017-03-23 18 views
0

シート範囲内の行数に基づいてユーザーフォームをプログラムで作成しています(テスト用に固定数に設定されています)。 次に、チェックするボックスをチェックし、コマンドボタンをクリックします。Excel VBAユーザーフォームのチェックボックスアクセス

以下のコードで実行されるuserformには、1つのコマンドボタンと1つのチェックボックスが手動で追加されています。他のチェックボックスはプログラムによって追加されます。 問題のあるチェックボックスから値を取得する方法を理解できません。 "testbox"が定義されていないというエラーが表示されます。 私は何か簡単なものを見逃していることを知っています...

どのような考えですか? ありがとうございました!

Option Explicit 

Private Sub updateTablesBtn_Click() 
    If CheckBox1.Value = True Then 
     MsgBox "true" 
    End If 

    If testBox.Value = True Then 
     MsgBox "true" 
    End If 
End Sub 

Private Sub UserForm_Initialize() 
    Dim chkBox As MSForms.CheckBox 

    With formTableUpdate 
     .Width = 150 
     .Height = 200 '15 + 20 * (noOfVariants + 1) + 30 
    End With 

    Set chkBox = formTableUpdate.Controls.Add("Forms.CheckBox.1") 
    With chkBox 
     .Name = "testBox" 
     .Caption = "test" 
     .Left = 5 
     .Top = 10 
    End With 

    With updateTablesBtn 
     .Caption = "Update Tables" 
     .Height = 25 
     .Width = 76 
     .Left = 38 
     .Top = 30 
    End With 

End Sub 

答えて

1

これを試してみてください:

Dim chkBox As Control 

For Each chkBox In formTableUpdate.Controls 
    If chkBox.Name = "testBox" Then 
     MsgBox chkBox.Caption & " has the value " & chkBox.Value 
    End If 
Next chkBox 
関連する問題