ExcelのVBA・マクロでセル書式を操作する方法まとめ

ExcelのVBA・マクロでセル書式を操作する方法をいくつか紹介します。

文字のフォントやサイズを変更する方法

ここでは、ExcelのVBA・マクロで文字の書式やサイズを変更する方法を紹介します。

ExcelのVBA・マクロで文字の書式やサイズを変更するには、Fontプロパティを変更します。

Sub test20()
Range(“A1”).Value = “グループ”
Range(“B1”).Value = “名前”
Range(“C1”).Value = “値”
Range(“A2”).Value = “A”
Range(“A3”).Value = “B”
Range(“A4”).Value = “C”
Range(“A5”).Value = “A”
Range(“A6”).Value = “B”
Range(“A7”).Value = “C”
Range(“B2”).Value = “高橋”
Range(“B3”).Value = “鈴木”
Range(“B4”).Value = “田中”
Range(“B5”).Value = “松井”
Range(“B6”).Value = “野村”
Range(“B7”).Value = “谷重”
Range(“C2:C7”).Value = 100

Range(“A1:C1”).Interior.Color = rgbYellow
Range(“A1:C1”).Font.Color = rgbRed
Range(“A1:C1”).HorizontalAlignment = xlCenter
Range(“A1:C7”).Borders.Weight = xlThin
Range(“A1:C7”).Font.Name = “Meiryo UI”
Range(“A1:C7”).Font.Size = 12

End Sub

ここでは、「Range(“A1:C7”).Font.Name = “Meiryo UI”」で、書式を変更しています。さらに「Range(“A1:C7”).Font.Size = 12」で文字サイズを変更しています。

文字を中央揃えに設定・変更する方法

ここでは、ExcelのVBA・マクロでセルの文字を中央揃えに設定・変更する方法を紹介します。

ExcelのVBA・マクロでセルの文字を中央揃えに設定・変更するには、HorizontalAlignment プロパティを変更します。

Sub test20()
Range(“A1”).Value = “グループ”
Range(“B1”).Value = “名前”
Range(“C1”).Value = “値”
Range(“A2”).Value = “A”
Range(“A3”).Value = “B”
Range(“A4”).Value = “C”
Range(“A5”).Value = “A”
Range(“A6”).Value = “B”
Range(“A7”).Value = “C”
Range(“B2”).Value = “高橋”
Range(“B3”).Value = “鈴木”
Range(“B4”).Value = “田中”
Range(“B5”).Value = “松井”
Range(“B6”).Value = “野村”
Range(“B7”).Value = “谷重”
Range(“C2:C7”).Value = 100

Range(“A1:C1”).Interior.Color = rgbYellow
Range(“A1:C1”).Font.Color = rgbRed
Range(“A1:C1”).HorizontalAlignment = xlCenter
Range(“A1:C7”).Borders.Weight = xlThin
Range(“A1:C7”).Font.Name = “Meiryo UI”
Range(“A1:C7”).Font.Size = 12

End Sub

ここでは、「Range(“A1:C1”).HorizontalAlignment = xlCenter」で、A1からC1までのセルの文字を中央揃えしています。左揃え、右揃えの場合は、設定する値をxlLeft、xlRightにそれぞれ変更します。

文字色を設定・変更する方法

ここでは、ExcelのVBA・マクロでセルの文字色を設定・変更する方法を紹介します。

ExcelのVBA・マクロでセルの文字色を設定・変更するには、Font.Colorプロパティを変更します。

Sub test20()
Range(“A1”).Value = “グループ”
Range(“B1”).Value = “名前”
Range(“C1”).Value = “値”
Range(“A2”).Value = “A”
Range(“A3”).Value = “B”
Range(“A4”).Value = “C”
Range(“A5”).Value = “A”
Range(“A6”).Value = “B”
Range(“A7”).Value = “C”
Range(“B2”).Value = “高橋”
Range(“B3”).Value = “鈴木”
Range(“B4”).Value = “田中”
Range(“B5”).Value = “松井”
Range(“B6”).Value = “野村”
Range(“B7”).Value = “谷重”
Range(“C2:C7”).Value = 100

Range(“A1:C1”).Interior.Color = rgbYellow
Range(“A1:C1”).Font.Color = rgbRed
Range(“A1:C1”).HorizontalAlignment = xlCenter
Range(“A1:C7”).Borders.Weight = xlThin
Range(“A1:C7”).Font.Name = “Meiryo UI”
Range(“A1:C7”).Font.Size = 12

End Sub

ここでは、「Range(“A1:C1”).Font.Color = rgbRed」で、A1からC1までのセルの文字色を赤色に設定しています。基本的な色はrgb+色の英語名で変更可能です。

背景色を設定・変更する方法

ここでは、ExcelのVBA・マクロでセルの背景色を設定・変更する方法を紹介します。

ExcelのVBA・マクロでセルの背景色を設定・変更するには、Interior.Colorプロパティを変更します。

Sub test20()
Range(“A1”).Value = “グループ”
Range(“B1”).Value = “名前”
Range(“C1”).Value = “値”
Range(“A2”).Value = “A”
Range(“A3”).Value = “B”
Range(“A4”).Value = “C”
Range(“A5”).Value = “A”
Range(“A6”).Value = “B”
Range(“A7”).Value = “C”
Range(“B2”).Value = “高橋”
Range(“B3”).Value = “鈴木”
Range(“B4”).Value = “田中”
Range(“B5”).Value = “松井”
Range(“B6”).Value = “野村”
Range(“B7”).Value = “谷重”
Range(“C2:C7”).Value = 100

Range(“A1:C1”).Interior.Color = rgbYellow
Range(“A1:C1”).Font.Color = rgbRed
Range(“A1:C1”).HorizontalAlignment = xlCenter
Range(“A1:C7”).Borders.Weight = xlThin
Range(“A1:C7”).Font.Name = “Meiryo UI”
Range(“A1:C7”).Font.Size = 12

End Sub

ここでは、「Range(“A1:C1”).Interior.Color = rgbYellow」で、A1からC1までのセルの背景色を黄色に塗りつぶしています。基本的な色はrgb+色の英語名で変更可能です。

セルの書式だけを削除・クリアする方法

ここでは、ExcelのVBA・マクロでセルの書式だけを削除・クリアする方法を紹介します。

ExcelのVBA・マクロでセルの書式だけを削除・クリアするには、ClearFormatsメソッドを使います。

<サンプル>
Sub test19()
Range(“A1”).Value = “グループ”
Range(“A2”).Value = “A”
Range(“A3”).Value = “B”
Range(“A4”).Value = “C”
Range(“A5”).Value = “A”
Range(“A6”).Value = “B”
Range(“A7”).Value = “C”
Range(“B1”).Value = “名前”
Range(“C1”).Value = “値”
Range(“B2”).Value = “高橋”
Range(“B3”).Value = “鈴木”
Range(“B4”).Value = “田中”
Range(“B5”).Value = “松井”
Range(“B6”).Value = “野村”
Range(“B7”).Value = “谷重”
Range(“C2:C7”).Value = 100

Range(“C5:C7”).ClearFormats

End Sub

ここでは、「Range(“C5:C7”).ClearFormats」で、C5からC7までのセルに設定された書式だけをクリアしています。