セルの塗りつぶしの設定について
どのような設定項目があるか,
VBAで設定する方法について見ていきます。
セルの塗りつぶしとは
セルの背景色や背景のパターンのことです。
手動で設定するには
右クリックメニュー[セルの書式設定]をクリックすると
表示されるウィンドウの
[塗りつぶし]タブでできます。

設定は大まかに
「背景色」と「パターン」に分かれています。
背景色は色,塗りつぶし効果の設定ができます。
パターンは色と種類を設定できます。
VBAでセルの塗りつぶしの設定をする方法を見ていきます。
セルの塗りつぶしは
Interiorオブジェクトが持つプロパティで設定します。
背景色は
ColorIndexプロパティかColorプロパティで指定します。
ColorIndexプロパティは1~56までの整数を指定,
ColorプロパティはRGB関数を使用して色を指定します。
TintAndShadeプロパティで
設定した色を明るく、または暗くすることができます。
-1から1の範囲で小数の値を指定します。
塗りつぶし効果はグラデーションの設定で
使用頻度は高くないので省略します。
パターンは
Patternプロパティで設定します。
設定できる種類は下記を参照ください。
記事「パターン一覧」
パターンの色は
PatternColorIndexやPatternColorで指定します。
PatternColorIndexプロパティは1~56までの整数を指定,
PatternColorプロパティはRGB関数を使用して色を指定します。
PatternTintAndShadeプロパティで
設定した色を明るく、または暗くすることができます。
-1から1の範囲で小数の値を指定します。
セルの塗りつぶしを設定するのは
主に表を見やすくする場合です。
一例として
表のタイトル行の区別,奇数行と偶数行の区別を付けるために
セルの塗りつぶしを設定します。
次のコードは選択範囲に対して
一行目に濃い水色,奇数行に薄い水色の背景色を設定します。
セルの背景色を設定するコード:
|
Sub macro20200614a() 'セルの背景色の設定
Dim i As Integer '罫線の設定------------------------------- With Selection.Borders(xlEdgeTop) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlMedium End With With Selection.Borders(xlEdgeBottom) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlMedium End With With Selection.Borders(xlEdgeLeft) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlMedium End With With Selection.Borders(xlEdgeRight) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlMedium 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 '左上のセルに右下がりの斜線を設定 With Selection.Cells(1, 1).Borders(xlDiagonalDown) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlThin End With '背景色の設定------------------------------- '1行目の設定 With Selection.Rows(1).Interior '塗りつぶしの色 .Color = RGB(100, 150, 220) .TintAndShade = 0 End With '奇数行の設定 For i = 3 To Selection.Rows.count Step 2 With Selection.Rows(i).Interior '塗りつぶしの色 .Color = RGB(100, 150, 220) .TintAndShade = 0.7 End With Next i
End Sub
|
実行結果:

次はコードは選択範囲に対して
一行目と奇数行に異なるパターンを設定します。
セルにパターンを設定するコード:
|
Sub macro20200614b() 'セルのパターンの設定
Dim i As Integer '罫線の設定------------------------------- With Selection.Borders(xlEdgeTop) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlMedium End With With Selection.Borders(xlEdgeBottom) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlMedium End With With Selection.Borders(xlEdgeLeft) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlMedium End With With Selection.Borders(xlEdgeRight) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlMedium 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 '左上のセルに右下がりの斜線を設定 With Selection.Cells(1, 1).Borders(xlDiagonalDown) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlThin End With 'パターンの設定------------------------------- '1行目 With Selection.Rows(1).Interior '塗りつぶしの色 .Pattern = xlPatternGray25 End With '奇数行 For i = 3 To Selection.Rows.count Step 2 With Selection.Rows(i).Interior '塗りつぶしの色 .Pattern = xlPatternGray8 End With Next i
End Sub
|
実行結果:

使用Ver:Win10, Excel For Office365
コメント