excel 基于另一个范围的VBA条件格式范围

jum4pzuy  于 2023-04-22  发布在  其他
关注(0)|答案(1)|浏览(107)

Hy.我试图根据另一个区域中单元格的值来设置区域中单元格的格式。如果单元格F18:Q18的值大于5%,我希望单元格F8:Q8为红色。如果F8-F18〉5%,F8应为红色,如果G8-G18〉5%,G8应为红色,依此类推,直到Q8。我使用的代码是:

Range("F8:Q8").Select
    Application.CutCopyMode = False
    Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
        "=ABS(F$18-F$8)>5%"
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Font
        .Bold = True
        .Italic = False
        .TintAndShade = 0
    End With
    With Selection.FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .Color = 255
        .TintAndShade = 0
    End With
    Selection.FormatConditions(1).StopIfTrue = False
    Selection.FormatConditions(1).ScopeType = xlSelectionScope

但这只使F8变红,而不是整个范围。你能帮我吗

xdnvmnnf

xdnvmnnf1#

Sub FormatRed()
    Dim rng As Range, cl As Range
    
    Set rng = Range("F8:Q8")
    
    For Each cl In rng
        If VBA.Abs(cl.Offset(10, 0) - cl) > 0.05 Then
            cl.Interior.Color = vbRed
        End If
    Next cl
End Sub

相关问题