1. 概要
VBA(Visual Basic for Applications)におけるRangeオブジェクトのFormatConditionsプロパティは、そのセル範囲に設定された条件付き書式(Conditional Formatting)のコレクションを表します。Excelの条件付き書式は、セルの値や数式に応じて自動的にセルの書式(フォント色や背景色など)を変更する機能です。FormatConditionsは、その条件付き書式ルールをプログラムから追加・変更・削除するために使います。
つまり、FormatConditionsを操作することで、VBAで動的に条件付き書式を設定したり編集したりでき、Excelの見た目やデータの可視化を自動化できます。
2. FormatConditionsの基本構造
Range.FormatConditionsは、FormatConditionsオブジェクトのコレクションです。- つまり、1つのセルまたはセル範囲には複数の条件付き書式を設定可能で、それぞれがFormatConditionオブジェクトとして格納されています。
- 例えば、A1セルに「赤背景の条件付き書式」と「フォントを太字にする条件付き書式」があれば、FormatConditionsコレクションの中に2つのFormatConditionオブジェクトがあります。
Dim fc As FormatCondition
For Each fc In Range("A1").FormatConditions
Debug.Print fc.Type
Next fc
3. FormatConditionsの主な用途
- 新規条件付き書式の追加
- 既存の条件付き書式の削除・変更
- 条件付き書式ルールの調査や管理
4. FormatConditionsの取得
Dim fcs As FormatConditions
Set fcs = Range("A1:A10").FormatConditions
fcsはその範囲に設定されている条件付き書式のコレクションを表します。
5. 新規条件付き書式の追加
FormatConditionsコレクションのAddメソッドで新しい条件付き書式を追加します。Addの書式は以下の通りです。
FormatConditions.Add(Type, Operator, Formula1, Formula2)
主な引数
- Type:条件付き書式のタイプ。以下が代表的です。
xlCellValue(1) : セルの値に基づく条件xlExpression(-4172) : 数式による条件xlColorScale(6) : カラースケールxlDatabar(5) : データバーxlIconSet(3) : アイコンセット
- Operator:比較演算子(
xlEqual,xlGreater,xlBetweenなど)。xlExpressionの場合は不要。 - Formula1:条件の基準値または数式。
- Formula2:範囲条件など2つ目の値。
例1:セルの値が100より大きい場合に赤文字にする条件付き書式の追加
With Range("A1:A10").FormatConditions
.Add Type:=xlCellValue, Operator:=xlGreater, Formula1:="100"
With .Item(.Count).Font
.Color = vbRed
End With
End With
例2:数式を使った条件付き書式の追加
With Range("B1:B10").FormatConditions
.Add Type:=xlExpression, Formula1:="=$B1>50"
With .Item(.Count).Interior
.Color = RGB(255, 255, 0) ' 黄色背景
End With
End With
6. FormatConditionオブジェクトの主なプロパティ
FormatConditionsコレクションの各要素はFormatConditionオブジェクトです。代表的なプロパティを挙げます。
- Type: 条件付き書式のタイプ(
xlCellValueやxlExpressionなど) - Operator: 条件の演算子
- Formula1: 条件に使う値または数式
- Formula2: 範囲指定の条件での2つ目の値
- Font: フォント書式を表すFontオブジェクト(色、スタイルなど)
- Interior: セルの塗りつぶしの書式を表すInteriorオブジェクト(背景色など)
- Borders: 罫線書式
- StopIfTrue: この条件が真の場合に他の条件を無視するか(True/False)
7. 既存条件付き書式の変更・削除
条件付き書式の削除
FormatConditionsコレクションのDeleteメソッドで全削除もできますし、個別に削除も可能です。
' 全削除
Range("A1:A10").FormatConditions.Delete
' 1つ目の条件付き書式だけ削除
Range("A1:A10").FormatConditions(1).Delete
変更例
既存の条件付き書式のフォント色を青に変更
Range("A1:A10").FormatConditions(1).Font.Color = vbBlue
8. 条件付き書式の優先順位とStopIfTrue
複数の条件付き書式がある場合、上から順に判定されます。StopIfTrueがTrueなら、該当条件が満たされた時点で以降の条件は判定されません。
With Range("A1:A10").FormatConditions(1)
.StopIfTrue = True
End With
9. カラースケールやデータバーなどの高度な条件付き書式
Excel 2007以降では、単純な値比較だけでなく、**カラースケール(ColorScale)、データバー(DataBar)、アイコンセット(IconSet)**など視覚的に見やすい条件付き書式が使えます。
これらはAddColorScale, AddDatabar, AddIconSetConditionメソッドで追加します。
例:3色カラースケールの設定
Dim cs As ColorScale
Set cs = Range("A1:A10").FormatConditions.AddColorScale(3)
With cs
.SetFirstPriority
.ColorScaleCriteria(1).Type = xlConditionValueLowestValue
.ColorScaleCriteria(1).FormatColor.Color = RGB(255, 0, 0) ' 赤
.ColorScaleCriteria(2).Type = xlConditionValuePercentile
.ColorScaleCriteria(2).Value = 50
.ColorScaleCriteria(2).FormatColor.Color = RGB(255, 255, 0) ' 黄
.ColorScaleCriteria(3).Type = xlConditionValueHighestValue
.ColorScaleCriteria(3).FormatColor.Color = RGB(0, 255, 0) ' 緑
End With
10. 注意点と補足
- 条件付き書式はExcelのバージョンや言語設定で動作やプロパティの挙動が異なることがあります。
- 複雑な条件付き書式をVBAで操作するときは、適用範囲や既存の条件の優先順位に注意が必要です。
- 条件付き書式の削除は慎重に行わないと、ユーザー設定の書式を消してしまう場合があります。
FormatConditionsを大量に操作する場合、パフォーマンスが低下することがあります。必要な範囲に限定するのが望ましいです。
11. まとめ
| ポイント | 内容 |
|---|---|
| FormatConditions | Rangeの条件付き書式の集合。複数の条件を管理できる。 |
| 新規追加 | Addメソッドでセルの値や数式に基づく条件付き書式を追加。 |
| 編集 | FormatConditionオブジェクトのFontやInteriorで書式変更可能。 |
| 削除 | Deleteメソッドで条件を削除できる。 |
| 複数条件の優先順位 | 上から順に判定。StopIfTrueで判定停止も可能。 |
| 高度な書式 | AddColorScaleなどでカラースケールやデータバーが設定可能。 |

