vba経由でExcelのフォント、背景の色を変更する方法のご紹介です。
マクロ・VBAの作り方自体がわからない方は下記をご参照ください。
![](https://sunagitsune.com/wp-content/uploads/2018/12/sea-2361247_1280-160x90.jpg)
ColorIndexプロパティの使い方
「対象」の「フォント」or「背景」の「色」を変更するという書式で記述します。FontをInteriorにすると背景色を変更することができますし、対象にはRangeの他にCellやSelectionなどを指定することもできます。
対象.Font.ColorIndex = 色番号
背景の色を変更する(色番号)
対象.Interior.ColorIndex = 色番号
※色番号0は「塗りつぶしなし」
![vbafontcl1](https://sunagitsune.com/wp-content/uploads/2019/03/vbafontcl1.jpg)
![vbafontcl2](https://sunagitsune.com/wp-content/uploads/2019/03/vbafontcl2-500x148.jpg)
Range(“B2”).Font.ColorIndex = 50
(B2のフォントの色を色番号50に変更する)と入力します。
![vbafontcl3](https://sunagitsune.com/wp-content/uploads/2019/03/vbafontcl3.jpg)
色番号とは
デフォルトでExcelについてるパレットの色で、56色用意されています。57以降はエラーになります。0は「塗りつぶしなし」(デフォルト値)になります。
1は黒、2は白、3は赤、4は緑……とそれぞれの数値に色が割り振られています。
上の表もマクロで作っています。ちょっと脱線しますが、A列に1~56の色番号を出力するだけならFor文を使った簡単なコードでできますので、ご参考までに書いておきます。
' vba
' 1から56まで繰り返し処理を実行
For i = 1 to 56
Range("A" & i).Interior.ColorIndex = i
Next i
背景色の変更をするのでInterior.ColorIndexを使用しています。For nextは1回処理を実行するたびに変数を1増やし、所定の数が終わったらループを終了する命令です。ここでは繰り返しの変数をiとして、そのiをセルの番号(A1,A2..)、色番号(ColorIndex = 1、2…)としても利用しています。
数値以外でColorIndexを指定
ColorIndexプロパティには定数として、xlColorIndexAutomaticとxlColorIndexNoneが用意されています(xlColorIndex列挙型定数)。
xlColorIndexAutomatic
![colorindex1](https://sunagitsune.com/wp-content/uploads/2019/03/colorindex1.jpg)
ためしにフォントと背景にそれぞれ適用してみると、
![colorindex2](https://sunagitsune.com/wp-content/uploads/2019/03/colorindex2.jpg)
![colorindex3](https://sunagitsune.com/wp-content/uploads/2019/03/colorindex3.jpg)
![colorindex4](https://sunagitsune.com/wp-content/uploads/2019/03/colorindex4.jpg)
xlColorIndexNone
![colorindex5](https://sunagitsune.com/wp-content/uploads/2019/03/colorindex5.jpg)
![colorindex6](https://sunagitsune.com/wp-content/uploads/2019/03/colorindex6.jpg)
![colorindex7](https://sunagitsune.com/wp-content/uploads/2019/03/colorindex7.jpg)
![colorindex8](https://sunagitsune.com/wp-content/uploads/2019/03/colorindex8.jpg)
Colorプロパティの使い方
こちらはRGBで色指定をする方法です。RGBでは引数の中に3つ値を指定しますが、それぞれRed、Green、Blueです。それぞれ0~255まで設定でき、値が大きくなるほどその色が強くなります。
対象.Font.Color = RGB(R,G,B)
背景の色を変更する(RGB)
対象.Interior.Color = RGB(R,G,B)
![vbafontcl11](https://sunagitsune.com/wp-content/uploads/2019/03/vbafontcl11-500x70.jpg)
(B2のフォントの色を灰色に変更)と入力、実行するとフォントの色が変更されます
RGBの確認方法・基本ルール
RGBについては「RGB」などで検索するのが一番お手軽ですが、パレット内でも確認することができます。
![vbafontcl4](https://sunagitsune.com/wp-content/uploads/2019/03/vbafontcl4.jpg)
![vbafontcl5](https://sunagitsune.com/wp-content/uploads/2019/03/vbafontcl5-500x347.jpg)
![vbafontcl6](https://sunagitsune.com/wp-content/uploads/2019/03/vbafontcl6-500x337.jpg)
![vbafontcl7](https://sunagitsune.com/wp-content/uploads/2019/03/vbafontcl7-500x346.jpg)
![vbafontcl8](https://sunagitsune.com/wp-content/uploads/2019/03/vbafontcl8-500x341.jpg)
![vbafontcl9](https://sunagitsune.com/wp-content/uploads/2019/03/vbafontcl9-500x344.jpg)
![vbafontcl10](https://sunagitsune.com/wp-content/uploads/2019/03/vbafontcl10.jpg)
元からある色に対して、明度だけを変更したい場合
TintAndShadeプロパティを使うことで、対象の明度だけを上下させることが可能です(数値は-1~1まで)。
対象.Font.TintAndShade = 数値(-1~1)
背景の明度を変更する
対象.Interior.TintAndShade = 数値(-1~1)
![tint2](https://sunagitsune.com/wp-content/uploads/2019/12/tint2.jpg)
![tint3](https://sunagitsune.com/wp-content/uploads/2019/12/tint3.jpg)
![tint4](https://sunagitsune.com/wp-content/uploads/2019/12/tint4.jpg)
色を直接指定するのではなく相対的に明度を上下させるので、アニメーションのような効果を持たせたり、日数がたつと段々セルが暗くなっていったり……といった効果を加えることが可能です。
色塗り応用編
一行ごとに色を塗る記事を作成しました。IfとFor Nextを使用します。
![](https://sunagitsune.com/wp-content/uploads/2020/03/cat-202071_1280-160x90.jpg)
VBAによる文字色・背景色の変更については以上です。
![](https://sunagitsune.com/wp-content/uploads/2021/03/person-6076771_1280-160x90.jpg)
![](https://sunagitsune.com/wp-content/uploads/2018/12/mathematics-1509559_1280-160x90.jpg)
コメント