VBA Excel 条件付き書式のルールをマクロで作成 数式で行を塗りつぶす
条件付き書式のルールをマクロで一気に流し込む
肝は、良い感じのカラーインデックスをルールの数だけ配列に入れる。繰り返しは減算のデクリメントで数式の数値が大きい順番になるように指定。ルールの番号はインクリメントで指定。カラーインデックス配列のアイテム番号はもインクリメントで指定してみた。配色は使い捨てのシートなのでテキトーに選んでる。
肝は、良い感じのカラーインデックスをルールの数だけ配列に入れる。繰り返しは減算のデクリメントで数式の数値が大きい順番になるように指定。ルールの番号はインクリメントで指定。カラーインデックス配列のアイテム番号はもインクリメントで指定してみた。配色は使い捨てのシートなのでテキトーに選んでる。
- 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
画像ルール
画像ルール書式


