HOME       LIST

【Excel】VBAで罫線を引く

VBAのコードでExcel表の罫線を引く


1.以下のようにしたい



2.VBAコード

シート名「出力」の指定した範囲を罫線で引く

Private Sub test()
    '範囲指定
    ThisWorkbook.Sheets("出力").Range("B2:D6").Select
    '上段
    With Selection.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .Weight = xlMedium
        .ColorIndex = xlAutomatic
    End With
    '左
    With Selection.Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .Weight = xlMedium
        .ColorIndex = xlAutomatic
    End With
    '右
    With Selection.Borders(xlEdgeRight)
        .LineStyle = xlContinuous
        .Weight = xlMedium
        .ColorIndex = xlAutomatic
    End With
    '下段
    With Selection.Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .Weight = xlMedium
        .ColorIndex = xlAutomatic
    End With
    '範囲内水平
    With Selection.Borders(xlInsideHorizontal)
        .LineStyle = xlDash
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    '範囲内垂直
    With Selection.Borders(xlInsideVertical)
        .LineStyle = xlDash
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    Cells(1, 1).Select
End Sub

指定した範囲を同じ罫線にする

Range(Cells(1, 1), Cells(10, 5).Borders.LineStyle = xlContinuous
or
Range(A1:J5).Borders.LineStyle = xlContinuous

参考URL

http://officetanaka.net/excel/vba/tips/tips51.htm
http://qiita.com/tomikiya/items/15153cc074d5745e4ac8
http://officetanaka.net/excel/vba/cell/cell07.htm

以前コンテンツ:【Excel】日付の曜日を表示する。
次のコンテンツ:【Excel】ExcelのプロセスがWindows タスクマネージャーから消えない時の対応



Copyright(c) 2007-2024 dojeun.com All Rights Reserved.

Valid XHTML 1.0 Transitional