セルの書式設定
VBA を使って Excel の操作を自動化する際、データの入力だけでなくセルの書式設定を行いたい場面は非常に多くあります。フォントの変更、背景色の設定、罫線の追加、表示形式の指定など、見やすい表を作成するうえで欠かせない操作です。
VBA では、Range オブジェクトの Font、Interior、Borders、NumberFormatLocal などのプロパティを通じて、Excel の「セルの書式設定」ダイアログで行える操作のほぼすべてをコードから制御できます。
この記事では、セルの書式設定に関する主要なプロパティの使い方から、実践的な活用例まで詳しく解説します。
フォントの設定 - Font オブジェクト
Range オブジェクトの Font プロパティを使用すると、セル内の文字のフォント名、サイズ、スタイル、色などを設定できます。
基本的なフォント設定
Sub SetFontBasic()
With Range("A1").Font
.Name = "游ゴシック" ' フォント名
.Size = 14 ' フォントサイズ
.Bold = True ' 太字
.Italic = False ' 斜体
.Underline = xlUnderlineStyleSingle ' 下線
End With
End Sub
主要な Font プロパティ一覧
| プロパティ | 説明 | 設定値の例 |
|---|---|---|
Name | フォント名 | "游ゴシック" / "MS Pゴシック" |
Size | フォントサイズ | 1 ~ 409 |
Bold | 太字 | True / False |
Italic | 斜体 | True / False |
Underline | 下線 | xlUnderlineStyleSingle / xlUnderlineStyleDouble / xlUnderlineStyleNone |
Strikethrough | 取り消し線 | True / False |
Color | 文字色(RGB) | RGB(255, 0, 0) / vbRed |
ColorIndex | 文字色(インデックス) | 1 ~ 56 / xlColorIndexAutomatic |
文字色の設定
文字色の設定には Color プロパティと ColorIndex プロパティの 2 つの方法があります。Color プロパティの方がより多くの色を表現できるため、通常はこちらを使用します。
Sub SetFontColor()
' RGB 関数で色を指定(フルカラー対応)
Range("A1").Font.Color = RGB(255, 0, 0) ' 赤色
Range("A2").Font.Color = RGB(0, 128, 0) ' 緑色
Range("A3").Font.Color = RGB(0, 0, 255) ' 青色
' VBA の組み込み定数で色を指定
Range("A4").Font.Color = vbRed
Range("A5").Font.Color = vbBlue
' ColorIndex で色を指定(最大 56 色)
Range("A6").Font.ColorIndex = 3 ' 赤色
Range("A7").Font.ColorIndex = 5 ' 青色
' 色をクリア(自動に戻す)
Range("A8").Font.ColorIndex = xlColorIndexAutomatic
End Sub
Color プロパティは RGB 関数を使い、約 1,677 万色から色を指定できます。一方 ColorIndex は 0
~ 56 のインデックス番号で最大 56 色のみです。新しいコードでは Color
プロパティの使用を推奨します。
一部の文字だけ書式を変更する
Characters プロパティを使うと、セル内の特定の文字だけに書式を適用できます。
Sub SetPartialFont()
Range("A1").Value = "重要なお知らせ"
' 「重要」の 2 文字だけ赤色・太字にする
With Range("A1").Characters(Start:=1, Length:=2).Font
.Color = RGB(255, 0, 0)
.Bold = True
End With
End Sub
背景色の設定 - Interior オブジェクト
Range オブジェクトの Interior プロパティを使用すると、セルの背景色(塗りつぶし)を設定できます。
背景色の設定方法
Sub SetInteriorColor()
' RGB 関数で背景色を設定
Range("A1").Interior.Color = RGB(255, 255, 200) ' 薄い黄色
Range("A2").Interior.Color = RGB(200, 230, 255) ' 薄い青色
' VBA の組み込み定数で設定
Range("A3").Interior.Color = vbYellow
' ColorIndex で設定
Range("A4").Interior.ColorIndex = 6 ' 黄色
' 背景色をクリア(塗りつぶしなし)
Range("A5").Interior.ColorIndex = xlColorIndexNone
End Sub
主要な Interior プロパティ一覧
| プロパティ | 説明 | 設定値の例 |
|---|---|---|
Color | 背景色(RGB) | RGB(255, 255, 0) / vbYellow |
ColorIndex | 背景色(インデックス) | 1 ~ 56 / xlColorIndexNone |
Pattern | パターン | xlPatternSolid / xlPatternGray50 |
PatternColor | パターンの色 | RGB(128, 128, 128) |
ThemeColor | テーマの色 | xlThemeColorAccent1 |
TintAndShade | 明るさの調整 | -1(暗い)~ 1(明るい) |
条件に応じた色分け
実務では、セルの値に応じて背景色を変更する処理が頻繁に行われます。
Sub ColorByValue()
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets(1)
Dim lastRow As Long
lastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
Dim i As Long
For i = 2 To lastRow
Dim score As Long
score = ws.Cells(i, 2).Value
' 点数に応じて背景色を変更
Select Case score
Case Is >= 80
ws.Cells(i, 2).Interior.Color = RGB(198, 239, 206) ' 緑(優秀)
Case Is >= 60
ws.Cells(i, 2).Interior.Color = RGB(255, 235, 156) ' 黄(合格)
Case Else
ws.Cells(i, 2).Interior.Color = RGB(255, 199, 206) ' 赤(不合格)
End Select
Next i
End Sub
条件付き書式(FormatConditions)を VBA
から設定する方法もありますが、単純な色分けであれば上記のように直接 Interior.Color
を設定する方がシンプルです。
罫線の設定 - Borders オブジェクト
Range オブジェクトの Borders プロパティを使用すると、セルの罫線を設定できます。Borders はコレクションで、上下左右や斜線の個別制御も可能です。
一括設定
Sub SetBordersAll()
With Range("A1:D5").Borders
.LineStyle = xlContinuous ' 実線
.Weight = xlThin ' 細い線
.Color = RGB(0, 0, 0) ' 黒色
End With
End Sub
罫線の位置を個別に指定
Borders コレクションにインデックスを指定することで、上下左右の罫線を個別に設定できます。
Sub SetBordersIndividual()
Dim rng As Range
Set rng = Range("A1:D5")
' 上辺
With rng.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlMedium
End With
' 下辺
With rng.Borders(xlEdgeBottom)
.LineStyle = xlDouble
.Weight = xlThick
End With
' 左辺
With rng.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlThin
End With
' 右辺
With rng.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlThin
End With
' 内側の横線
With rng.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.Weight = xlHairline
.Color = RGB(180, 180, 180)
End With
' 内側の縦線
With rng.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.Weight = xlHairline
.Color = RGB(180, 180, 180)
End With
End Sub
罫線の位置定数一覧
| 定数 | 説明 |
|---|---|
xlEdgeTop | 上辺 |
xlEdgeBottom | 下辺 |
xlEdgeLeft | 左辺 |
xlEdgeRight | 右辺 |
xlInsideHorizontal | 内側の横線 |
xlInsideVertical | 内側の縦線 |
xlDiagonalDown | 左上から右下への斜線 |
xlDiagonalUp | 左下から右上への斜線 |
罫線のスタイルと太さ
| LineStyle 定数 | 説明 |
|---|---|
xlContinuous | 実線 |
xlDash | 破線 |
xlDashDot | 一点鎖線 |
xlDashDotDot | 二点鎖線 |
xlDot | 点線 |
xlDouble | 二重線 |
xlLineStyleNone | なし |
| Weight 定数 | 説明 |
|---|---|
xlHairline | 極細 |
xlThin | 細い |
xlMedium | 中 |
xlThick | 太い |
罫線の削除
Sub ClearBorders()
' 範囲内のすべての罫線を削除
Range("A1:D5").Borders.LineStyle = xlLineStyleNone
' 特定の辺だけ削除
Range("A1:D5").Borders(xlEdgeBottom).LineStyle = xlLineStyleNone
End Sub
表示形式の設定 - NumberFormatLocal
NumberFormatLocal プロパティを使用すると、セルの表示形式(数値の桁区切り、日付の表示形式、パーセントなど)を設定できます。
代表的な表示形式
Sub SetNumberFormat()
' 数値(桁区切り)
Range("A1").NumberFormatLocal = "#,##0"
' 数値(小数点以下 2 桁)
Range("A2").NumberFormatLocal = "#,##0.00"
' 通貨
Range("A3").NumberFormatLocal = "¥#,##0"
' パーセント
Range("A4").NumberFormatLocal = "0.0%"
' 日付
Range("A5").NumberFormatLocal = "yyyy/mm/dd"
Range("A6").NumberFormatLocal = "yyyy年m月d日"
' 時刻
Range("A7").NumberFormatLocal = "hh:mm:ss"
' ゼロ埋め(5 桁)
Range("A8").NumberFormatLocal = "00000"
' 文字列
Range("A9").NumberFormatLocal = "@"
' 標準に戻す
Range("A10").NumberFormatLocal = "G/標準"
End Sub
NumberFormat は英語ロケールの書式を使用し、NumberFormatLocal
は日本語ロケールの書式を使用します。日本語環境では NumberFormatLocal
を使う方がわかりやすく、Excel の「セルの書式設定」ダイアログと同じ書式文字列を使用できます。
配置の設定
セル内の文字の配置(水平方向・垂直方向)やインデント、テキスト制御も VBA から設定できます。
水平方向と垂直方向の配置
Sub SetAlignment()
With Range("A1")
' 水平方向の配置
.HorizontalAlignment = xlCenter ' 中央揃え
' 垂直方向の配置
.VerticalAlignment = xlCenter ' 上下中央揃え
End With
End Sub
| 水平方向の定数 | 説明 |
|---|---|
xlGeneral | 標準 |
xlLeft | 左揃え |
xlCenter | 中央揃え |
xlRight | 右揃え |
xlJustify | 両端揃え |
| 垂直方向の定数 | 説明 |
|---|---|
xlTop | 上揃え |
xlCenter | 中央揃え |
xlBottom | 下揃え |
テキスト制御
Sub SetTextControl()
With Range("A1")
.WrapText = True ' 折り返して全体を表示
.ShrinkToFit = False ' 縮小して全体を表示
.MergeCells = False ' セルの結合
.Orientation = 0 ' 文字の角度(-90 ~ 90)
.IndentLevel = 2 ' インデントレベル
End With
End Sub
WrapText(折り返し)と ShrinkToFit(縮小)は同時に True
にできません。両方を設定すると、後から設定した方が有効になります。
実践的な活用例
表のヘッダーを書式設定する
実務で最も頻繁に行われる操作の一つが、表のヘッダー行に書式を適用する処理です。
Sub FormatTableHeader()
Dim headerRange As Range
Set headerRange = Range("A1:E1")
With headerRange
' フォント設定
.Font.Bold = True
.Font.Color = RGB(255, 255, 255)
.Font.Size = 11
' 背景色
.Interior.Color = RGB(68, 114, 196)
' 罫線
.Borders.LineStyle = xlContinuous
.Borders.Weight = xlThin
.Borders.Color = RGB(255, 255, 255)
' 配置
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
' 行の高さ
.RowHeight = 30
End With
End Sub
一覧表全体を整形する
ヘッダーとデータ領域の書式を一括で適用し、見やすい表を作成する汎用プロシージャです。
Sub FormatAsTable()
Dim ws As Worksheet
Set ws = ActiveSheet
' データ範囲を取得
Dim lastRow As Long
lastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
Dim lastCol As Long
lastCol = ws.Cells(1, ws.Columns.Count).End(xlToLeft).Column
Dim dataRange As Range
Set dataRange = ws.Range(ws.Cells(1, 1), ws.Cells(lastRow, lastCol))
' ========== 全体のリセット ==========
dataRange.Font.ColorIndex = xlColorIndexAutomatic
dataRange.Interior.ColorIndex = xlColorIndexNone
dataRange.Borders.LineStyle = xlLineStyleNone
' ========== ヘッダー行 ==========
Dim headerRange As Range
Set headerRange = ws.Range(ws.Cells(1, 1), ws.Cells(1, lastCol))
With headerRange
.Font.Bold = True
.Font.Color = RGB(255, 255, 255)
.Interior.Color = RGB(55, 96, 146)
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.RowHeight = 28
End With
' ========== データ行 ==========
Dim i As Long
For i = 2 To lastRow
Dim rowRange As Range
Set rowRange = ws.Range(ws.Cells(i, 1), ws.Cells(i, lastCol))
' 交互に背景色を設定(ストライプ)
If i Mod 2 = 0 Then
rowRange.Interior.Color = RGB(221, 235, 247)
Else
rowRange.Interior.Color = RGB(255, 255, 255)
End If
rowRange.VerticalAlignment = xlCenter
Next i
' ========== 罫線 ==========
' 外枠
With dataRange.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlMedium
.Color = RGB(55, 96, 146)
End With
With dataRange.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlMedium
.Color = RGB(55, 96, 146)
End With
With dataRange.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlMedium
.Color = RGB(55, 96, 146)
End With
With dataRange.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlMedium
.Color = RGB(55, 96, 146)
End With
' 内側の横線
If lastRow > 1 Then
With dataRange.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.Weight = xlHairline
.Color = RGB(180, 198, 220)
End With
End If
' 内側の縦線
If lastCol > 1 Then
With dataRange.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.Weight = xlHairline
.Color = RGB(180, 198, 220)
End With
End If
' ========== 列幅の自動調整 ==========
dataRange.Columns.AutoFit
End Sub
大量のセルに書式を設定する場合は、Application.ScreenUpdating = False
を処理の前に、Application.ScreenUpdating = True
を処理の後に設定すると、描画の更新が抑制されて処理速度が大幅に向上します。
書式のコピー
あるセルの書式を別のセルにコピーしたい場合、PasteSpecial メソッドを使用します。
Sub CopyFormat()
' A1 セルの書式を B1:B10 にコピー
Range("A1").Copy
Range("B1:B10").PasteSpecial Paste:=xlPasteFormats
' クリップボードをクリア
Application.CutCopyMode = False
End Sub
書式のクリア
セルの書式をすべてクリアして初期状態に戻すには、ClearFormats メソッドを使用します。
Sub ClearAllFormats()
' 書式のみクリア(値は残る)
Range("A1:D10").ClearFormats
' 値も含めてすべてクリア
' Range("A1:D10").Clear
End Sub
色の指定方法まとめ
VBA でセルの色を指定する方法は複数あります。用途に応じて使い分けましょう。
| 方法 | 指定例 | 色数 | 用途 |
|---|---|---|---|
RGB 関数 | RGB(255, 0, 0) | 約 1,677 万色 | 最も自由度が高い |
| VBA 定数 | vbRed / vbBlue | 8 色 | 基本色を素早く指定 |
ColorIndex | 3(赤) | 56 色 | レガシーコードとの互換性 |
ThemeColor | xlThemeColorAccent1 | テーマ依存 | テーマに合わせた配色 |
VBA の組み込み色定数
| 定数 | 色 | RGB 値 |
|---|---|---|
vbBlack | 黒 | RGB(0, 0, 0) |
vbRed | 赤 | RGB(255, 0, 0) |
vbGreen | 緑 | RGB(0, 255, 0) |
vbBlue | 青 | RGB(0, 0, 255) |
vbYellow | 黄 | RGB(255, 255, 0) |
vbMagenta | マゼンタ | RGB(255, 0, 255) |
vbCyan | シアン | RGB(0, 255, 255) |
vbWhite | 白 | RGB(255, 255, 255) |
練習問題
まとめ
- Font プロパティでフォント名、サイズ、太字、色などを設定する
- Interior プロパティで背景色(塗りつぶし)を設定する
- Borders プロパティで罫線のスタイル、太さ、色を設定する。位置の個別指定も可能
- NumberFormatLocal プロパティで表示形式(桁区切り、日付、パーセントなど)を設定する
- 色の指定には
RGB関数が最も自由度が高く、推奨される ClearFormatsで書式のみクリア、PasteSpecial xlPasteFormatsで書式のみコピーが可能- 大量のセルを処理する場合は
Application.ScreenUpdating = Falseでパフォーマンスを向上させる