VBA Excel 条件付き書式のルールをマクロで作成 数式で行を塗りつぶす

Quick Edit Pencil
条件付き書式のルールをマクロで一気に流し込む

肝は、良い感じのカラーインデックスをルールの数だけ配列に入れる。繰り返しは減算のデクリメントで数式の数値が大きい順番になるように指定。ルールの番号はインクリメントで指定。カラーインデックス配列のアイテム番号はもインクリメントで指定してみた。配色は使い捨てのシートなのでテキトーに選んでる。
  • arrColorIndex = Array(34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55)
  • For d = UBound(arrColorIndex) To 1 Step -1
  • xlExpression, Formula1:="=IF(AND(ISNUMBER($G1),$G1>=" & d * 100 & ")

注意:このマクロは既存のルールを削除するので、テストは .FormatConditions.Delete をコメント化してやる

画像シート

マクロはシートモジュールに書いといた(マクロの記録をいじった)
Private Sub 条件付き書式設定()
Dim d As Long
Dim i As Long
i = 1
Dim arrColorIndex As Variant
arrColorIndex = Array(34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55)
    With Me.Range("$A:$H")
        .FormatConditions.Delete
        For d = UBound(arrColorIndex) To 1 Step -1
            .FormatConditions.Add Type:=xlExpression, Formula1:="=IF(AND(ISNUMBER($G1),$G1>=" & d * 100 & "),TRUE,FALSE)"
            With .FormatConditions(i).Borders(xlLeft)
                .LineStyle = xlContinuous
                .ThemeColor = 1
                .TintAndShade = -0.249946592608417
                .Weight = xlThin
            End With
            With .FormatConditions(i).Borders(xlRight)
                .LineStyle = xlContinuous
                .ThemeColor = 1
                .TintAndShade = -0.249946592608417
                .Weight = xlThin
            End With
            With .FormatConditions(i).Borders(xlTop)
                .LineStyle = xlContinuous
                .ThemeColor = 1
                .TintAndShade = -0.249946592608417
                .Weight = xlThin
            End With
            With .FormatConditions(i).Borders(xlBottom)
                .LineStyle = xlContinuous
                .ThemeColor = 1
                .TintAndShade = -0.249946592608417
                .Weight = xlThin
            End With
            With .FormatConditions(i).Interior
                .PatternColorIndex = xlAutomatic
                .ColorIndex = arrColorIndex(d)
                .TintAndShade = 0.599981688894314
            End With
            i = i + 1
        Next
    End With
End Sub
画像ルール
画像ルール書式

このブログの人気の投稿

PowerShellのGetDetailsOf メソッドでプロパティの詳細情報のID番号と項目名を列挙します

書字方向 横書方向変換スクリプト 左書きから右書きへ(コピペ用途)

PowerShellで複数ファイルのプロパティを取得する方法(準備編)

DOMノードオブジェクトを文字列に変換する

簡単 YouTube動画をダウンロード、音声のみ保存する方法 2019

AppleScript 改行 コード 置換

Blender: 辺の長さを数値で指定するアドオン

Powershell: プロパティの詳細情報インデックスと項目名の列挙