Run-time error '9' Subscript out of range with Conditional Format code

后端 未结 1 1375
既然无缘
既然无缘 2021-01-25 01:38

I\'m very new to VBA (and any sort of programming in general), so I\'m not sure how to proceed here. I\'m guessing my error has something to do with overlapping ranges for my c

相关标签:
1条回答
  • 2021-01-25 02:01

    There are two problems with your code:

    1. You only delete the conditional formats for the first range - but add conditions to all ranges - and later access a specific one that most likely is not the one you just created (FormatConditions(3))
    2. The formulas you entered are the default english formulas - for some stange reason, FormatConditions.Add requires the local formulas though.

    I reworked your code, take a look if it solves your problem:

    Sub test2()
    
        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
    
        With fctApply(rng:=Range("$g$20:$g$1000"), strFormulaR1C1:=0, intType:=xlCellValue, intOperator:=xlLess, intColorIndex:=3)
            .Interior.Color = RGB(204, 204, 255)
            .Interior.Pattern = xlSolid
        End With
    
        With 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
        End With
    End Sub
    
    Private Function fctApply(rng As Range, _
        strFormulaR1C1 As Variant, _
        Optional intType As XlFormatConditionType = xlExpression, _
        Optional intOperator As XlFormatConditionOperator, _
        Optional intColorIndex As Integer = -1, _
        Optional dblRGB As Double = -1, _
        Optional blnDeleteOldConditions As Boolean = False _
        ) As FormatCondition
    
        Dim objCond As FormatCondition
        Dim strFormula As String
    
        If blnDeleteOldConditions Then rng.FormatConditions.Delete
    
        strFormula = Application.ConvertFormula(strFormulaR1C1, xlR1C1, xlA1)
    
        On Error GoTo ConvertLocal
        If intOperator <> 0 Then
            rng.FormatConditions.Add Type:=intType, _
                Formula1:=strFormula, Operator:=intOperator
        Else
            rng.FormatConditions.Add Type:=intType, _
                Formula1:=strFormula
        End If
        On Error GoTo 0
        Set objCond = rng.FormatConditions(rng.FormatConditions.Count)
        If intColorIndex <> -1 Then
            objCond.Font.ColorIndex = intColorIndex
        ElseIf dblRGB <> -1 Then
            objCond.Font.Color = dblRGB
        End If
        Set fctApply = objCond
    
        Exit Function
    ConvertLocal:
        With Range("A1") 'change this to an empty cell address - it is temporarily used to translate from local to normal formulas
            .Formula = strFormula
            strFormula = .FormulaLocal
            .Formula = ""
        End With
        Resume
    End Function
    
    0 讨论(0)
提交回复
热议问题