セルの罫線の設定について
設定できる罫線の位置,線種,太さ,色の指定方法と
VBAで設定する場合の実例を見ていきます。
設定できる罫線の位置は
セルの上下左右+右下がりの斜め線・右上がりの斜め線です。
VBAで設定するには
Borderオブジェクトの引数に罫線の位置を指定します。
設定できる罫線の位置は下記です。

線種は線なしを含めて8種類です。
それぞれの罫線に
LineStyleプロパティで線種を指定できます。
設定できる線種は下記になります。

線の太さは4種類です。
それぞれの罫線にWeightプロパティで指定します。

色はColorIndexプロパティか
Colorプロパティで指定します。
特に指定しない場合はなしでOKです。
ColorIndexプロパティは1~56までの数値を指定,
ColorプロパティはRGB関数を使用して色を指定します。
TintAndShadeプロパティで
設定した色を明るく、または暗くすることができます。
-1から1までの範囲で小数の値を指定します。
VBAによる罫線の設定の実例として
下図のような罫線を設定します。

コードはこちら
セルの罫線を設定するコード:
|
Sub macro20200615a() '罫線の設定 Selection.Borders(xlDiagonalDown).LineStyle = xlNone Selection.Borders(xlDiagonalUp).LineStyle = xlNone With Selection.Borders(xlEdgeTop) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlMedium .Color = RGB(255, 0, 0) End With With Selection.Borders(xlEdgeBottom) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlMedium .Color = RGB(255, 0, 0) End With With Selection.Borders(xlEdgeLeft) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlMedium .Color = RGB(255, 0, 0) End With With Selection.Borders(xlEdgeRight) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlMedium .Color = RGB(255, 0, 0) End With With Selection.Borders(xlInsideVertical) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlThin End With With Selection.Borders(xlInsideHorizontal) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlThin End With '一行目のセルの下線を2重線にする With Selection.Rows(1).Borders(xlEdgeBottom) .LineStyle = xlDouble .ColorIndex = 0 .TintAndShade = 0 .Weight = xlThick End With '一列目のセルの右線を2重線にする With Selection.Columns(1).Borders(xlEdgeRight) .LineStyle = xlDouble .ColorIndex = 0 .TintAndShade = 0 .Weight = xlThick End With '左上のセルに右下がりの斜線を設定 With Selection.Cells(1, 1).Borders(xlDiagonalDown) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlThin End With
End Sub
|
使用Ver:Win10, Excel For Office365
コメント