hiko-blog

VBA業務改善

MENU

罫線(実線のみ)

Sub 罫線()

    Dim ws As Worksheet
    Dim LastRow As Long
    Dim LastCol As Long
    Dim i As Long
    Dim RangeToFormat As Range

    ' シートを指定(例: シート1)
    Set ws = ThisWorkbook.Sheets("Sheet1")

    ' 最終行(データがある最後の行)を取得
    LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row

    ' 最終列(データがある最後の列)を取得
    LastCol = ws.Cells(1, ws.Columns.Count).End(xlToLeft).Column

    ' 縦線はすべて実線
    For i = 1 To LastRow
        Set RangeToFormat = ws.Range(ws.Cells(i, 1), ws.Cells(i, LastCol))
        
        ' 左縦線と右縦線を実線に設定
        With RangeToFormat.Borders(xlEdgeLeft)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .Weight = xlThin
        End With
        With RangeToFormat.Borders(xlEdgeRight)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .Weight = xlThin
        End With
    Next i

    ' 横線をすべて実線に設定
    For i = 1 To LastRow
        Set RangeToFormat = ws.Range(ws.Cells(i, 1), ws.Cells(i, LastCol))

        ' 上下の横線をすべて実線に設定
        With RangeToFormat.Borders(xlEdgeTop)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .Weight = xlThin
        End With
        With RangeToFormat.Borders(xlEdgeBottom)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .Weight = xlThin
        End With
    Next i

End Sub