Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel VBA-条件格式,索引不包括在间隔中_Excel_Vba - Fatal编程技术网

Excel VBA-条件格式,索引不包括在间隔中

Excel VBA-条件格式,索引不包括在间隔中,excel,vba,Excel,Vba,我有一段代码,它给我的错误索引不包括在.FormatConditions(Selection.FormatConditions.Count)行中的间隔中。SetFirstPriority With students.Range(MyNegRange) .FormatConditions.Add Type:=xlCellValue, Operator:=xlBetween, _ Formula1:="="&qu

我有一段代码,它给我的错误索引不包括在
.FormatConditions(Selection.FormatConditions.Count)行中的间隔中。SetFirstPriority

  With students.Range(MyNegRange)
 
    .FormatConditions.Add Type:=xlCellValue, Operator:=xlBetween, _
                          Formula1:="=""x""", Formula2:="=""x"""
    
    .FormatConditions(Selection.FormatConditions.Count).SetFirstPriority

    With Selection.FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .Color = 13311 'rosso
        .TintAndShade = 0
    End With

    .FormatConditions(1).StopIfTrue = False
 
  End With

我想将包含“x”的单元格涂成红色,但仅在特定范围内。我使用了宏记录器,然后尝试调整代码,我不知道为什么会出现错误。

您可以通过按
格式条件使代码更可读。将
添加到
With
块中:

与students.Range(MyNegRange).FormatConditions.Add(_
类型:=xlCellValue,运算符:=xlBetween_
公式1:=“=”“x”“”,公式2:=“=”“x”“”)
.SetFirstPriority
带.内饰
.PatternColorIndex=xlAutomatic
.Color=13311
.TintAndShade=0
以
.StopIfTrue=False
以

这样,代码的其余部分就被限定在刚刚添加的
FormatCondition
对象的范围内

删除
选择项
(它的两个实例)。很好,现在可以完美地工作了!