xlUniqueValues 的 VBA 条件格式语法

xlUniqueValues 的 VBA 条件格式语法

我正在尝试使用 VBA 下的条件格式突出显示重复项。

如果你看看使用 VBA 进行条件格式,FormatConditions.Add 的 XlFormatConditionType(类型)之一为 xlUniqueValues。

语法:FormatConditions.Add(类型,运算符,公式1,公式2)

列出的运算符为:xlBetween;xlEqual;xlGreater;xlGreaterEqual;xlLess;xlLessEqual;xlNotBetween;xlNotEqual。

这些都不适用于 xlUniqueValues。我尝试了所有方法,包括 xlDuplicate 和 xlUnique,它们不会抛出错误。类型 xlTextString 使用 TextOperator,xlTimePeriod 使用 DateOperator 作为运算符修饰符,但没有关于 xlUniqueValues 修饰符的信息。

With Range("a1:a10").FormatConditions.Add(xlTextString, TextOperator:=xlContains, String:="egg")
     .Interior.Color = RGB(255, 166, 0)
End With

我的代码:

With Range("B6:B30").FormatConditions.Add(xlUniqueValues, xlDuplicate)
     .Interior.Color = RGB(255, 166, 0)
End With

无论我为运算符输入什么,这都会突出显示所有唯一值。语法是正确的,但我找不到有关如何突出显示重复项的信息。

突出显示重复项的正确语法是什么?

我将其作为宏捕获条件格式(如下所示)工作,但我想让它使用这种语法,因为它更干净。

Range("B6:B30").Select
Selection.FormatConditions.AddUniqueValues
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
Selection.FormatConditions(1).DupeUnique = xlDuplicate
With Selection.FormatConditions(1).Interior
    .PatternColorIndex = xlAutomatic
    .Color = RGB(255, 166, 0)
    .TintAndShade = 0
End With

答案1

它可能更干净,但正如您已经发现的那样,它不起作用。格式条件.添加在这种情况下,运算符参数将被忽略:

可以是以下 XlFormatConditionOperator 常量之一:xlBetween、xlEqual、xlGreater、xlGreaterEqual、xlLess、xlLessEqual、xlNotBetween 或 xlNotEqual。如果 Type 为 xlExpression,则 Operator 参数将被忽略。

唯一值文档指出:

UniqueValues 对象使用 DupeUnique 属性返回或设置一个枚举,该枚举确定规则是否应在范围内查找重复值或唯一值。

这意味着如果你想检查重复项,请使用重复独特财产:

返回或设置 XlDupeUnique 枚举的常量之一,指定条件格式规则是否查找唯一值或重复值。

您的代码将更改为:

With Range("B6:B30").FormatConditions.Add(xlUniqueValues)
     .DupeUnique = xlDuplicate
     .Interior.Color = RGB(255, 166, 0)
End With

或者

With Range("B6:B30").FormatConditions.AddUniqueValues
     .DupeUnique = xlDuplicate
     .Interior.Color = RGB(255, 166, 0)
End With

相关内容