我录制了一个宏来使用vba有条件地格式化excel中的某些行,但是当我尝试再次运行它时,或者当我尝试将其循环以动态选择范围时,它会将单元格绘制成错误的颜色并且在错的方向。我希望细胞从红色到白色,从高到低,但是代码将细胞从橙色变为白色,从低到高。这是我的代码:
Sub ConForm()
Sheets("Percentage").Activate
Dim lastRow, currRow As Integer
Dim rng As Range
lastRow = Sheets("Percentage").Range("B250").End(xlUp).Row
currRow = 9
For currRow = 9 To (lastRow - 1)
'Range("F" & currRow & ":AL" & currRow).Select
Set rng = Sheets("Percentage").Range("F9:AL9") '"rng_ConForm" & currRow)
rng.Select
rng.FormatConditions.AddColorScale ColorScaleType:=2
rng.FormatConditions(1).ColorScaleCriteria(1).Type = _
xlConditionValueLowestValue
With rng.FormatConditions(1).ColorScaleCriteria(1).FormatColor
.Color = RGB(255, 255, 255)
.TintAndShade = 0
End With
rng.FormatConditions(1).ColorScaleCriteria(2).Type = _
xlConditionValueHighestValue
With rng.FormatConditions(1).ColorScaleCriteria(2).FormatColor
.Color = RGB(255, 0, 0)
.TintAndShade = 0
End With
rng.FormatConditions(1).ScopeType = xlSelectionScope
Next currRow
End Sub
有谁知道这里发生了什么?感谢
编辑:这是运行后的数据。在第一次运行之前,所有条件格式都被清除
A)运行一次后
B)运行两次后