📊 Excel VBA Top100
🎨

SetBorderStyle

書式設定

選択範囲に外枠と内側の罫線を設定します

🎬 デモGIF準備中

📖 使い方

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

💻 VBAコード

' SetBorderStyle
' -----------------
' Macro Name: SetBorderStyle
' Description: 選択範囲に外枠と内側の罫線を設定します
' Parameters: なし
' Returns: なし
' Usage: 対象範囲を選択してから SetBorderStyle を実行します
' -----------------

Sub SetBorderStyle()
    On Error GoTo ErrorHandler

    Dim rng As Range

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

    Set rng = Selection

    With rng.Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .Weight = xlThin
    End With
    With rng.Borders(xlEdgeRight)
        .LineStyle = xlContinuous
        .Weight = xlThin
    End With
    With rng.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .Weight = xlThin
    End With
    With rng.Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .Weight = xlThin
    End With
    With rng.Borders(xlInsideVertical)
        .LineStyle = xlContinuous
        .Weight = xlHairline
    End With
    With rng.Borders(xlInsideHorizontal)
        .LineStyle = xlContinuous
        .Weight = xlHairline
    End With

    MsgBox "罫線を設定しました", vbInformation, "完了"

    Exit Sub

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