0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Excel マクロ チェックボックス(activateX コントロール)を使用した処理分岐

Last updated at Posted at 2020-12-26

◇マクロのチェックボックス(activateX コントロール)を使用した処理分岐

〇事前準備

 ・シートにチェックボックス(activateX コントロール)を配置
 ・シートに処理ボタンを配置
 ・各種チェックボックスのオブジェクト名、Captionを分かりやすいように編集
  (編集は「開発」タブ→「デザインモード」→対象選択して「プロパティ」) 
  今回は以下のように設定
   ボタン オブジェクト名 caption
    k   checkBox_k   k
    y   checkBox_y   y
    s   checkBox_s   s
    m1   checkBox_m1  m1
    m2   checkBox_m2  m2

jizen1.PNG

jizen2.PNG

〇ソース

Option Explicit

Const SHEET_NAME_MAIN = "sheet1"

Sub selectSheetProcess()

Dim st_k, st_y, st_s, st_m1, st_m2 As String

'mainシートをactive
Worksheets(SHEET_NAME_MAIN).Activate

'以下チェックボックスの状態で処理分岐
'チェックボックスの状態を取得し、〇 or × を変数に格納
'checkBox_k
If ActiveSheet.checkBox_k.Value = True Then
st_k = "〇"
ElseIf ActiveSheet.checkBox_k.Value = False Then
st_k = "×"
End If

'checkBox_y
If ActiveSheet.checkBox_y.Value = True Then
st_y = "〇"
ElseIf ActiveSheet.checkBox_y.Value = False Then
st_y = "×"
End If

'checkBox_s
If ActiveSheet.checkBox_s.Value = True Then
st_s = "〇"
ElseIf ActiveSheet.checkBox_s.Value = False Then
st_s = "×"
End If

'checkBox_m1
If ActiveSheet.checkBox_m1.Value = True Then
st_m1 = "〇"
ElseIf ActiveSheet.checkBox_m1.Value = False Then
st_m1 = "×"
End If

'checkBox_m2
If ActiveSheet.checkBox_m2.Value = True Then
st_m2 = "〇"
ElseIf ActiveSheet.checkBox_m2.Value = False Then
st_m2 = "×"
End If

'状態の取得結果を文字連結
MsgBox st_k & st_y & st_s & st_m1 & st_m2

End Sub

〇実行

 ・全てチェックされている場合
res1.PNG
 
 ・k, s, m2のみチェック
res2.PNG

〇結果

 チェックボタンの状態が取得できる!

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?