excel VBA中基于不同单元格的条件格式设置

wi3ka0sx  于 2023-03-04  发布在  其他
关注(0)|答案(1)|浏览(192)

我正在编写一个宏,它是一个大项目的一部分。最后一点是,如果W列满足条件,则对V列中的单元格应用条件格式。
我得到
运行时错误5:无效的过程调用或参数错误。

Sub ComConFormGreen()
'
' ComConForGreen Macro
'

'
    Columns("W:W").Select
    Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, _
        Formula1:="=""Each"""
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Font
        .Color = -16752384
        .TintAndShade = 0
    End With
    With Selection.FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .Color = 13561798
        .TintAndShade = 0
    End With
    Selection.FormatConditions(1).StopIfTrue = False
    Columns("V:V").Select
    Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, _
        Formula1:="=""$W1=""Each"""""
        Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Font
        .Color = -16752384
        .TintAndShade = 0
    End With
    With Selection.FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .Color = 13561798
        .TintAndShade = 0
    End With
    Selection.FormatConditions(1).StopIfTrue = False
End Sub

问题似乎出现在以下行中:

Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, _
            Formula1:="=""$W1=""Each"""""
ddhy6vgd

ddhy6vgd1#

条件规则在xlCellValue中查找值,但您尝试解析公式,因此应改用xlExpression

Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=$W1=""Each"""

相关问题