📊 Excel VBA Top100
🎨

AlternateRowColors

書式設定

選択範囲の行に交互の背景色を設定します

🎬 デモGIF準備中

📖 使い方

  1. VBAエディタを開く(Alt + F11)
  2. モジュールを挿入(挿入 > モジュール)
  3. 下記VBAコードをコピー&ペースト
  4. ブック上で実行(Alt + F8 でマクロ選択)
💡 実行例: 対象範囲を選択してから AlternateRowColors を実行します

💻 VBAコード

' AlternateRowColors
' -----------------
' Macro Name: AlternateRowColors
' Description: 選択範囲の行に交互の背景色を設定します
' Parameters: なし
' Returns: なし
' Usage: 対象範囲を選択してから AlternateRowColors を実行します
' -----------------

Sub AlternateRowColors()
    On Error GoTo ErrorHandler

    Dim rng As Range
    Dim cell As Range
    Dim firstRow As Long

    If TypeName(Selection) <> "Range" Then
        MsgBox "セル範囲を選択してから実行してください", vbCritical, "エラー"
        Exit Sub
    End If

    Set rng = Selection
    firstRow = rng.Row

    For Each cell In rng.Rows
        If (cell.Row - firstRow) Mod 2 = 0 Then
            cell.Interior.Color = RGB(204, 229, 255)
        Else
            cell.Interior.Color = RGB(255, 255, 255)
        End If
    Next cell

    MsgBox "交互の背景色を設定しました", vbInformation, "完了"

    Exit Sub

ErrorHandler:
    MsgBox "エラーが発生しました:" & vbCrLf & Err.Description, vbCritical, "エラー"
End Sub