Excel VBA 逆引き集 | データバー・アイコンセット設定

Excel VBA
スポンサーリンク

データバー・アイコンセット設定

見た目で「大小・良否」を直感的に伝えるなら、条件付き書式のデータバーとアイコンセットが最短です。初心者が迷わないように、最小コードから色やしきい値のカスタマイズ、削除・再適用までをテンプレートでまとめます。基本は Range.FormatConditions に AddDatabar と AddIconSetCondition を追加します。


基本:データバーを設定する

Sub DataBar_Basic()
    Dim rng As Range, db As DataBar
    Set rng = Range("E3:E20")
    Set db = rng.FormatConditions.AddDatabar

    With db
        .BarColor.Color = RGB(0, 112, 192) '青
        .MinPoint.Modify xlConditionValueAutomaticMin
        .MaxPoint.Modify xlConditionValueAutomaticMax
    End With
End Sub
VB
  • ポイント:
    • 最短設定: AddDatabar → BarColor → Min/Max を自動に。
    • 自動最小/最大: 範囲の最小値・最大値でバー長を自動調整。
    • 色変更: BarColor.Color に RGB を指定。

応用:データバーの見た目と挙動を細かく調整

Sub DataBar_Advanced()
    Dim db As DataBar
    Set db = Range("F3:F50").FormatConditions.AddDatabar

    With db
        .BarColor.Color = RGB(198, 239, 206) '淡い緑
        .BarBorder.Type = xlDataBarBorderSolid '枠線あり
        .AxisPosition = xlDataBarAxisAutomatic '負値がある場合の軸位置
        .NegativeBarFormat.ColorType = xlDataBarColor '負値バー色を指定
        .NegativeBarFormat.Color = RGB(255, 199, 206) '負値用の淡い赤

        'しきい値を固定値で指定(例:0〜100のスコア)
        .MinPoint.Modify xlConditionValueNumber, 0
        .MaxPoint.Modify xlConditionValueNumber, 100
    End With
End Sub
VB
  • ポイント:
    • 枠線: BarBorder.Type でバーに境界線を追加。
    • 負値の表示: AxisPosition と NegativeBarFormat で赤バーと軸を整える。
    • 固定しきい値: Min/Max を「数値」「パーセンタイル」「パーセント」で指定可能。

基本:アイコンセットを設定する

Sub IconSet_Basic()
    Dim rng As Range
    Set rng = Range("G3:G20")

    With rng.FormatConditions.AddIconSetCondition
        .IconSet = ActiveWorkbook.IconSets(xl3TrafficLights1) '信号3色
        .ShowIconOnly = False  'アイコン+値を表示
        .ReverseOrder = False  '高い値ほど良いアイコン
    End With
End Sub
VB
  • ポイント:
    • IconSet選択: xl3TrafficLights1 など標準アイコンを選ぶ。
    • 表示: ShowIconOnly=True で値を隠してアイコンだけの表示にできる。
    • 並び: ReverseOrder=True でアイコンの意味を逆転。

応用:アイコンのしきい値を自分で決める

Sub IconSet_CustomThresholds()
    Dim ic As IconSetCondition
    Set ic = Range("H3:H50").FormatConditions.AddIconSetCondition

    With ic
        .IconSet = ActiveWorkbook.IconSets(xl3Symbols2) '丸○△×風の3段階
        .ShowIconOnly = True

        'しきい値タイプと値を設定(3段階:1,2,3番目)
        With .IconCriteria(2)
            .Type = xlConditionValueNumber '中間の境界
            .Value = 70                    '70以上で中アイコン以上
        End With

        With .IconCriteria(3)
            .Type = xlConditionValueNumber '上位の境界
            .Value = 90                    '90以上で最上位アイコン
        End With
    End With
End Sub
VB
  • ポイント:
    • IconCriteria: しきい値は2番目と3番目で設定(xl3系の場合)。
    • タイプ: 数値、パーセンタイル、パーセントから選べる。
    • 見せ方: ShowIconOnly で評価だけを“記号化”できる。

例題で練習

例題1:売上列にデータバー(最大値100万、負値は赤)

Sub Example_SalesDataBar()
    Dim db As DataBar
    Set db = Range("E3:E100").FormatConditions.AddDatabar
    With db
        .BarColor.Color = RGB(0, 176, 80)
        .MinPoint.Modify xlConditionValueNumber, 0
        .MaxPoint.Modify xlConditionValueNumber, 1000000
        .NegativeBarFormat.Color = RGB(255, 0, 0)
        .AxisPosition = xlDataBarAxisAutomatic
    End With
End Sub
VB

例題2:達成率に3色アイコン(80%/95%で区切る)

Sub Example_RateIcons()
    Dim ic As IconSetCondition
    Set ic = Range("F3:F100").FormatConditions.AddIconSetCondition
    With ic
        .IconSet = ActiveWorkbook.IconSets(xl3TrafficLights2)
        .ShowIconOnly = False
        With .IconCriteria(2)
            .Type = xlConditionValueNumber
            .Value = 0.8   '80%以上は黄色以上
        End With
        With .IconCriteria(3)
            .Type = xlConditionValueNumber
            .Value = 0.95  '95%以上は緑
        End With
    End With
End Sub
VB

例題3:テーブルのデータ列に一括適用(最終行が変動)

Sub Example_ApplyToDynamicRange()
    Dim last As Long, rng As Range
    last = Cells(Rows.Count, "E").End(xlUp).Row
    Set rng = Range("E3:E" & last)

    'データバー
    Dim db As DataBar: Set db = rng.FormatConditions.AddDatabar
    db.BarColor.Color = RGB(0, 112, 192)

    'アイコンセット(達成度)
    Dim ic As IconSetCondition: Set ic = rng.Offset(0, 1).FormatConditions.AddIconSetCondition
    ic.IconSet = ActiveWorkbook.IconSets(xl3Symbols)
End Sub
VB

ルールの削除・置き換えテンプレート

Sub ClearConditionalFormatting()
    '範囲の条件付き書式を全削除
    Range("E3:G100").FormatConditions.Delete
End Sub

Sub ReplaceDataBar()
    Dim rng As Range: Set rng = Range("E3:E100")
    rng.FormatConditions.Delete           '既存ルールを消す
    rng.FormatConditions.AddDatabar.BarColor.Color = RGB(255, 127, 39)
End Sub
VB
  • ポイント:
    • 上書き時: 期待通りの表示にするため、既存ルールを削除してから追加するのが安全。
    • 範囲限定: 必要な列だけ消す。シート全体削除は避ける。

実務の落とし穴と対策

  • 値の範囲が広すぎる: 自動Min/Maxだと極端な外れ値で見え方が崩れる。固定しきい値やパーセンタイルで安定化。
  • 負値の表示がわかりづらい: AxisPosition と NegativeBarFormat を必ず調整。
  • しきい値の意図が伝わらない: アイコンセットはしきい値コメントを表の見出しや注記に明示。
  • ルールが重複する: 追加前に FormatConditions.Delete で既存をクリアし、適用範囲を最小化。
  • テーブルと競合: テーブルの自動スタイルと混ざると見栄えが崩れることあり。必要ならテーブルスタイルを薄くするか、範囲へ適用。
タイトルとURLをコピーしました