Sub CheckSpecialAddition(oEvent As Object)
    On Error Resume Next ' 安全対策

    Dim oSheet As Object
    Dim oCell As Object
    Dim userCol As Long
    Dim dayRow As Long
    Dim useCount As Long
    Dim addCount As Long
    Dim val As String
    Dim maxAdd As Long
    Dim i As Long
    Dim totalCol As Long

    oSheet = ThisComponent.CurrentController.ActiveSheet
    oCell = ThisComponent.CurrentSelection

    If IsNull(oCell) Then Exit Sub

    userCol = oCell.RangeAddress.StartColumn
    dayRow = oCell.RangeAddress.StartRow

    ' A列(日付)や1行目(利用者名)はスキップ
    If userCol = 0 Or dayRow = 0 Then Exit Sub

    ' 利用日・加算回数をカウント
    For i = 1 To 31
        Dim checkCell As Object
        checkCell = oSheet.getCellByPosition(i, dayRow)
        Select Case Trim(checkCell.String)
            Case "○"
                useCount = useCount + 1
            Case "◎"
                useCount = useCount + 1
                addCount = addCount + 1
        End Select
    Next i

    ' 利用日数に応じた上限
    If useCount <= 5 Then
        maxAdd = 2
    ElseIf useCount <= 11 Then
        maxAdd = 4
    Else
        maxAdd = 6
    End If

    ' 入力内容を確認
    val = Trim(oCell.String)

    ' ◎を入力し、上限を超えたら警告して戻す
    If val = "◎" And addCount > maxAdd Then
        MsgBox "加算上限(" & maxAdd & "回)を超えています。入力できません。", 48, "入力制限"
        oCell.String = "○"
        oCell.CellBackColor = RGB(255,255,255)
        addCount = addCount - 1 ' 戻した分減らす
    ElseIf val = "◎" Then
        oCell.CellBackColor = RGB(255,255,0)   ' 黄色
    ElseIf val = "○" Then
        oCell.CellBackColor = RGB(255,255,255) ' 白
    ElseIf val = "" Then
        oCell.CellBackColor = RGB(255,255,255)
    End If

    ' ✅ 加算合計数を右端(列 33番目=AG列)に表示
    totalCol = 33  ' 列番号(A=0, B=1,... AG=32)→33でAH列にしたい場合は+1
    Dim totalCell As Object
    totalCell = oSheet.getCellByPosition(totalCol, dayRow)
    totalCell.Value = addCount
End Sub