1

我对 VBA(以及任何类型的编程)都很陌生,所以我不确定如何在这里进行。我猜我的错误与我的条件格式的重叠范围有关,因为当代码设置为不同的方式时,一旦范围不再重叠,我也会遇到错误。这可能不是这里的情况,但我认为这会有所帮助。

我收到以下代码的“下标超出范围”错误:

Sub test2()
    Dim rngToFormat As Range
    Set rngToFormat = ActiveSheet.Range("$a$1:$z$1000")
    Dim rngToFormat2 As Range
    Set rngToFormat2 = ActiveSheet.Range("$k$20:$k$1000")
    Dim rngToFormat3 As Range
    Set rngToFormat3 = ActiveSheet.Range("$j$22:$j$1000")
    Dim rngToFormat4 As Range
    Set rngToFormat4 = ActiveSheet.Range("$i$22:$i$1000")
    Dim rngToFormat5 As Range
    Set rngToFormat5 = ActiveSheet.Range("$g$20:$g$1000")
    Dim rngToFormat6 As Range
    Set rngToFormat6 = ActiveSheet.Range("$d$9, $f$9")
    Dim rngToFormat7 As Range
    Set rngToFormat7 = ActiveSheet.Range("$G$3:$G$7,$G$11:$G$15,$E$3:$E$7,$E$11:$E$15,$N$3:$N$7,$N$11:$N$15,$L$3:$L$7,$L$11:$L$15")
    rngToFormat.FormatConditions.Delete
    rngToFormat.FormatConditions.Add Type:=xlExpression, _
        Formula1:="=if(R[]C20=1, true(), false())"
        rngToFormat.FormatConditions(1).Font.Color = RGB(228, 109, 10)
    rngToFormat2.FormatConditions.Add Type:=xlExpression, _
        Formula1:="=and(R[]C7=""6. Negotiate"", R[]C11<25)"
        rngToFormat2.FormatConditions(2).Font.ColorIndex = 3
    rngToFormat2.FormatConditions.Add Type:=xlExpression, _
        Formula1:="=and(R[]C7=""4. Develop"", R[]C11<15)"
        rngToFormat2.FormatConditions(3).Font.ColorIndex = 3
    rngToFormat2.FormatConditions.Add Type:=xlExpression, _
        Formula1:="=and(R[]C7=""5. Prove"", R[]C11<20)"
        rngToFormat2.FormatConditions(4).Font.ColorIndex = 3
    rngToFormat2.FormatConditions.Add Type:=xlExpression, _
        Formula1:="=and(R[]C7=""7. Committed"", R[]C11<30)"
        rngToFormat2.FormatConditions(5).Font.ColorIndex = 3
    rngToFormat2.FormatConditions.Add Type:=xlExpression, _
        Formula1:="=and(R[]C7=""Closed Won"", R[]C11<35)"
        rngToFormat2.FormatConditions(6).Font.ColorIndex = 3
    rngToFormat3.FormatConditions.Add Type:=xlCellValue, _
        Operator:=xlGreater, Formula1:=200
        rngToFormat3.FormatConditions(7).Font.ColorIndex = 3
    rngToFormat4.FormatConditions.Add Type:=xlCellValue, _
        Operator:=xlGreater, Formula1:=60
        rngToFormat4.FormatConditions(8).Font.ColorIndex = 3
    rngToFormat5.FormatConditions.Add Type:=xlExpression, _
        Formula1:="=or(R[]C7=""1. Plan"", R[]C7=""2. Create"", R[]C7=""3. Qualify"")"
        rngToFormat5.FormatConditions(9).Font.ColorIndex = 3
    rngToFormat6.FormatConditions.Add Type:=xlCellValue, _
        Operator:=xlLess, Formula1:=0
        rngToFormat6.FormatConditions(10).Font.ColorIndex = 3
        rngToFormat6.FormatConditions(10).Interior.Color = RGB(204, 204, 255)
        rngToFormat6.FormatConditions(10).Interior.Pattern = xlSolid
    rngToFormat7.FormatConditions.Add Type:=xlCellValue, _
        Operator:=xlLess, Formula1:=0
        rngToFormat7.FormatConditions(11).Font.ColorIndex = 3
        rngToFormat7.FormatConditions(11).Interior.Color = RGB(215, 228, 158)
        rngToFormat7.FormatConditions(11).Interior.Pattern = xlSolid
End Sub

任何建议将不胜感激,谢谢!

4

1 回答 1

1

您的代码有两个问题:

  1. 您只删除第一个范围的条件格式 - 但将条件添加到所有范围 - 然后访问一个很可能不是您刚刚创建的特定格式 ( FormatConditions(3))
  2. 您输入的公式是默认的英文公式 - 出于某种原因,FormatConditions.Add需要本地公式。

我修改了你的代码,看看它是否解决了你的问题:

子测试2()

    fctApply rng:=Range("$a$1:$z$1000"), strFormulaR1C1:="=(R[]C20=1)", dblRGB:=RGB(228, 109, 10), blnDeleteOldConditions:=True

    fctApply rng:=Range("$k$20:$k$1000"), strFormulaR1C1:="=and(R[]C7=""6.Negotiate"",R[]C11<25)", intColorIndex:=3
    fctApply rng:=Range("$k$20:$k$1000"), strFormulaR1C1:="=and(R[]C7=""4. Develop"", R[]C11<15)", intColorIndex:=3
    fctApply rng:=Range("$k$20:$k$1000"), strFormulaR1C1:="=and(R[]C7=""5. Prove"", R[]C11<20)", intColorIndex:=3
    fctApply rng:=Range("$k$20:$k$1000"), strFormulaR1C1:="=and(R[]C7=""7. Committed"", R[]C11<30)", intColorIndex:=3
    fctApply rng:=Range("$k$20:$k$1000"), strFormulaR1C1:="=and(R[]C7=""Closed Won"", R[]C11<35)", intColorIndex:=3

    fctApply rng:=Range("$j$22:$j$10000"), strFormulaR1C1:=200, intType:=xlCellValue, intOperator:=xlGreater, intColorIndex:=3

    fctApply rng:=Range("$i$22:$i$1000"), strFormulaR1C1:=60, intType:=xlCellValue, intOperator:=xlGreater, intColorIndex:=3

    使用 fctApply(rng:=Range("$g$20:$g$1000"), strFormulaR1C1:=0, intType:=xlCellValue, intOperator:=xlLess, intColorIndex:=3)
        .Interior.Color = RGB(204, 204, 255)
        .Interior.Pattern = xlSolid
    结束于

    使用 fctApply(rng:=Range("$G$3:$G$7,$G$11:$G$15,$E$3:$E$7,$E$11:$E$15,$N$3:$N$7,$ N$11:$N$15,$L$3:$L$7,$L$11:$L$15"), strFormulaR1C1:=0, intType:=xlCellValue, intOperator:=xlLess, intColorIndex:=3)
        .Interior.Color = RGB(215, 228, 158)
        .Interior.Pattern = xlSolid
    结束于
结束子

私有函数 fctApply(rng As Range, _
    strFormulaR1C1 作为变体,_
    可选的 intType As XlFormatConditionType = xlExpression, _
    可选的 intOperator 作为 XlFormatConditionOperator, _
    可选的 intColorIndex As Integer = -1, _
    可选 dblRGB As Double = -1, _
    可选的 blnDeleteOldConditions As Boolean = False _
    ) 作为格式条件

    将 objCond 调暗为 FormatCondition
    将 strFormula 调暗为字符串

    If blnDeleteOldConditions Then rng.FormatConditions.Delete

    strFormula = Application.ConvertFormula(strFormulaR1C1, xlR1C1, xlA1)

    出错时转到 ConvertLocal
    如果 intOperator <> 0 那么
        rng.FormatConditions.Add 类型:=intType,_
            公式1:=strFormula,运算符:=intOperator
    别的
        rng.FormatConditions.Add 类型:=intType,_
            公式1:=strFormula
    万一
    错误转到 0
    设置 objCond = rng.FormatConditions(rng.FormatConditions.Count)
    如果 intColorIndex <> -1 那么
        objCond.Font.ColorIndex = intColorIndex
    ElseIf dblRGB <> -1 则
        objCond.Font.Color = dblRGB
    万一
    设置 fctApply = objCond

    退出函数
本地转换:
    使用 Range("A1") ' 将其更改为空单元格地址 - 它暂时用于从本地公式转换为普通公式
        .Formula = strFormula
        strFormula = .FormulaLocal
        .Formula = ""
    结束于
    恢复
结束功能
于 2013-02-04T12:37:34.090 回答