【ExcelVBA】罫線設定まとめ

①罫線を引く箇所を選択

VBA
'上罫線
Cells.Borders(xlEdgeTop)

'下罫線
Cells.Borders(xlEdgeBottom)

'左罫線
Cells.Borders(xlEdgeLeft)

'右罫線
Cells.Borders(xlEdgeRight)

'格子
Cells.Borders

'外枠
Cells.BorderAround

'内側横罫線
Cells.Borders(xlInsideHorizontal)

'内側縦罫線
Cells.Borders(xlInsideVertical)

②オプションを設定

【LineStyle】

種類
xlContinuous実線
xlDash破線
xlDashDot一点鎖線
xlDashDotDot二点鎖線
xlDot点線
xlDouble二重線
xlSlantDashDot斜め斜線
xlLineStyleNone無し

【Weight】

太さ
xlHairline極細
xlThin
xlMedium
xlThick

【Color】

記述方法例(赤色)
RGBRGB(255,0,0)
ColorConstantsvbRed
XlRgbColor rgbRed

【オプション規定値】
LineStyle:xlContinuous
Weight:xlThin
Color:vbBlack
※いずれか1つのオプション指定が必要

記述例

VBA
'------------bodersの記述------------
'格子 + 実線 + 細 + 黒色
Cells.Borders.LineStyle = xlContinuous
'格子 + 二点鎖線 + 中 + 赤色
With Cells.Borders
    .LineStyle = xlDashDotDot
    .Weight = xlMedium
    .Color:=vbRed
End With

'------------BoderAroundの記述------------※記述方法が異なる
'外枠 + 実線 + 細 + 黒色
Cells.BorderAround LineStyle = xlContinuous
'外枠 + 二点鎖線 + 中 + 赤色
Cells.BorderAround LineStyle:=xlDashDotDot, Weight:=xlMedium, Color:=vbRed