Private Sub CommandButton1_Click() Dim chkBoxes() As Control Dim i As Integer Dim j As Integer Dim isChecked As Boolean Dim index As Integer isChecked = False
' 各大項目の小項目の数 Dim subItems() As Integer
' チェックボックスの数を合計して配列を初期化 Dim totalCheckboxes As Integer totalCheckboxes = 0 For i = LBound(subItems) To UBound(subItems) totalCheckboxes = totalCheckboxes + subItems(i) Next i ReDim chkBoxes(1 To totalCheckboxes)
' 配列にチェックボックスを格納 index = 1 For i = 1 To UBound(subItems) + 1
Set chkBoxes(index) = Me.Controls(i & "_" & j) index = index + 1 Next j Next i
' 配列内のチェックボックスを確認
If chkBoxes(i).Value = True Then isChecked = True Exit For End If Next i
' 全てのチェックボックスが未チェックの場合の処理 If Not isChecked Then MsgBox "少なくとも一つのチェックボックスを選択してください。", vbExclamation, "選択エラー" End If End Sub