分享

vba条件格式简化案例

 新用户7567z8wq 2022-03-31

用宏录制的条件格式很繁杂,以下是几个简化的实例

案例一 :'正红负绿

   Union(Range(Cells(2, 17), Cells(row2 - 1 + y, 17)), Range(Cells(2, 19), Cells(row2 - 1 + y, 19))).Select

        With Selection

            .FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, Formula1:="=0"    

            .FormatConditions(1).Font.Color = -16776961

            .FormatConditions.Add Type:=xlCellValue, Operator:=xlLess, Formula1:="=0"                                                              .FormatConditions(2).Font.Color = -11489280

        End With

 案例二:  '红B绿S

   Columns("R:R").Select                                                                 

        With Selection

            .FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, Formula1:=B"""

            .FormatConditions(1).Font.Color = -16383844

            .FormatConditions(1).Interior.Color = 13551615

            .FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, Formula1:=S"""

            .FormatConditions(2).Font.Color = -16752384

            .FormatConditions(2).Interior.Color = 13561798

        End With

案例三

    Range(Cells(2, 3), Cells(row1, 3)).Select

        Selection.FormatConditions.AddTop10

        With Selection.FormatConditions(1)

            .TopBottom = xlTop10Top

            .Rank = 4                                                                       '前4红色

            .Font.Color = -16383844

            .Interior.Color = 13551615

        End With

        Selection.FormatConditions.AddTop10

        With Selection.FormatConditions(2)

            .TopBottom = xlTop10Bottom

            .Rank = 4                                                                       '后4绿色

            .Font.Color = -16752384

            .Interior.Color = 13561798

        End With

案例四:

    Range("U2:U18").Select

        Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=and(U2>$W$1,C2<140,right(B2,1)<>""!"")"    '三条件公式

        Selection.FormatConditions(1).Font.Color = -16383844

        Selection.FormatConditions(1).Interior.Color = 10284031

案例五:起始行为变量

    Range("B2:B10").Select

        Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=and(U2>$W$1,AA2>0,k2<0.5)"      

        Selection.FormatConditions(1).Font.Color = -16776961    '红字

    Range(Cells(cgs + 2, "B"), Cells(30, "B")).Select

        Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=or(BB" & cgs + 2 & ">13,BB" & cgs + 2 & "=0)"     

        Selection.FormatConditions(2).Interior.Color = 65535       '黄底

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多