ray88’s diary

お仕事で困ったとき用の自分用の覚書

Excel VBA オプションボタン(ラジオボタン)の状態を確認する

f:id:ray88:20201015211233p:plain

Private Sub CommandButton1_Click()
    Dim i As Long
    Dim strResult As String
    Dim wb As Workbook
    
    Set wb = ThisWorkbook
    '--------------------------------------------------------
    'オプションボタンの状態を確認する
    For i = 1 To 3
        If Controls("OptionButton" & i).Value = True Then
            strResult = Controls("OptionButton" & i).Caption
        End If
    Next
    '---------------------------------------------------------
    If strResult = "" Then
        MsgBox "実行タイプを選択してください"
    Else     
     wb.Sheets("Sheet1").Range("B3") = strResult
     wb.Save
     wb.Close     
     Set wb = Nothing
      Unload Me    
    End If
End Sub

Private Sub UserForm_Initialize()
   'ブックを開かずにフォームのみ開く
    Application.Visible = False
End Sub

Private Sub UserForm_Terminate()
’もとに戻す
    Application.Visible = True
End Sub

ray88.hatenablog.com