【问题标题】:Excel Macro to apply conditional formattingExcel 宏应用条件格式
【发布时间】:2021-08-25 03:10:31
【问题描述】:

这是我的第一个宏项目,我正试图弄清楚如何修复它。 我想将黄色突出显示到范围内的同一列,但在运行后突出显示范围内的所有内容。对我有什么见解吗?

这就是我想看到的。

事情就是这样。

这是我复制的代码

https://www.bluepecantraining.com/portfolio/excel-vba-macro-to-apply-conditional-formatting-based-on-value/

然后修改它。

Sub test()

    Dim rg As Range
    Dim cond1 As FormatCondition, cond2 As FormatCondition
    Set rg = Range("$C$4:$AG$14")

    'clear any existing conditional formatting
    rg.FormatConditions.Delete

    'define the rule for each conditional format
    Set cond1 = rg.FormatConditions.Add(xlCellValue, xlEqual, "weekday(C$5)=7")
    Set cond2 = rg.FormatConditions.Add(xlCellValue, xlEqual, "weekday(C$5)=1")


    'define the format applied for each conditional format
    With cond1
        rg.Interior.Color = vbYellow
        rg.Font.Color = vbBlack
    End With

    With cond2
        rg.Interior.Color = vbYellow
        rg.Font.Color = vbBlack
    End With

End Sub

【问题讨论】:

    标签: excel vba conditional-formatting


    【解决方案1】:
    1. Type 参数更改为xlExpression
    2. 在您的With 语句中,您应该删除rg。您正在做的是将格式应用于范围 (rg) 本身,而不是 FormatCondition 对象 (cond1/cond2)。
    Sub test()
    
        Dim rg As Range
        Dim cond1 As FormatCondition, cond2 As FormatCondition
        Set rg = Range("$C$4:$AG$14")
    
        'clear any existing conditional formatting
        rg.FormatConditions.Delete
    
        'define the rule for each conditional format
        Set cond1 = rg.FormatConditions.Add(xlExpression, xlEqual, "=WEEKDAY(C$5)=7")
        Set cond2 = rg.FormatConditions.Add(xlExpression, xlEqual, "=WEEKDAY(C$5)=1")
    
    
        'define the format applied for each conditional format
        With cond1
            .Interior.Color = vbYellow
            .Font.Color = vbBlack
        End With
    
        With cond2
            .Interior.Color = vbYellow
            .Font.Color = vbBlack
        End With
    
    End Sub
    

    【讨论】:

    • 如果某个答案解决了您的问题,您可以通过单击答案旁边的勾号来接受它。 (如果提交了多个答案,您只能接受一个答案)。 @DEEK
    • 我接受了。我刚开始stackflow,我不知道。再次感谢雷蒙德。祝你有美好的一天。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-10
    • 2020-11-16
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多