Windows 10 Pro_x64 + Excel2013_x86 VBA で Frame + OptionButton を使う

Windows 10 Pro_x64 + Excel2013_x86 VBA で Frame + OptionButton を使う

 

Frame + OptionButton はペアで使うことが多いのだけど

・OptionButtonクリックイベントで モジュールプライベートの変数にステータスをセット

・CommandButtonクリックイベントでステータスを取得する

だと、前者を選ぶ気がするんだけどな・・

MSのドキュメントで Controls の説明見つからなかったんだけど

どこにあるんだろう??(あるのかな)

 

・UserFormを1個追加する

・Frameを1個追加する

・Frameの上にOptionButtonを4個追加する

・CommandButtonを1個追加する

Option Explicit

Private Sub UserForm_Initialize()
    '
    '   初期値の設定
    '
    OptionButton1.Value = True
End Sub

Private Sub OptionButton1_Click()
    Debug.Print "OptionButton1:" & OptionButton1.Value
    Debug.Print "OptionButton2:" & OptionButton2.Value
    Debug.Print "OptionButton3:" & OptionButton3.Value
    Debug.Print "OptionButton4:" & OptionButton4.Value
End Sub

Private Sub OptionButton2_Click()
    Debug.Print "OptionButton1:" & OptionButton1.Value
    Debug.Print "OptionButton2:" & OptionButton2.Value
    Debug.Print "OptionButton3:" & OptionButton3.Value
    Debug.Print "OptionButton4:" & OptionButton4.Value
End Sub

Private Sub OptionButton3_Click()
    Debug.Print "OptionButton1:" & OptionButton1.Value
    Debug.Print "OptionButton2:" & OptionButton2.Value
    Debug.Print "OptionButton3:" & OptionButton3.Value
    Debug.Print "OptionButton4:" & OptionButton4.Value
End Sub

Private Sub OptionButton4_Click()
    Debug.Print "OptionButton1:" & OptionButton1.Value
    Debug.Print "OptionButton2:" & OptionButton2.Value
    Debug.Print "OptionButton3:" & OptionButton3.Value
    Debug.Print "OptionButton4:" & OptionButton4.Value
End Sub

Private Sub CommandButton1_Click()
'
    Dim varCtrl As Variant
'
    For Each varCtrl In Controls
        '
        '   Like "OptionButton[1-4]"
        '       :OptionButton1~ OptionButton4 に一致
        '
        If varCtrl.Name Like "OptionButton[1-4]" Then
'Debug.Print varCtrl.Name, varCtrl.Value
            '
            '   varCtrl.Name  :OptionButton1~OptionButton4 に一致
            '   varCtrl.Value :True/False
            '
            Select Case varCtrl.Name
            Case "OptionButton1"
                    If varCtrl.Value Then
                        Debug.Print "Checked OptionButton1:" & OptionButton1.Value
                    End If
            Case "OptionButton2"
                    If varCtrl.Value Then
                        Debug.Print "Checked OptionButton2:" & OptionButton2.Value
                    End If
            Case "OptionButton3"
                    If varCtrl.Value Then
                        Debug.Print "Checked OptionButton3:" & OptionButton3.Value
                    End If
            Case "OptionButton4"
                    If varCtrl.Value Then
                        Debug.Print "Checked OptionButton4:" & OptionButton4.Value
                    End If
            End Select
        End If
    Next

End Sub