簡體   English   中英

運行時錯誤“ 9”下標超出條件格式代碼范圍

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

我對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

任何建議,將不勝感激,謝謝!

您的代碼有兩個問題:

  1. 您只刪除第一個范圍的條件格式-但將條件添加到所有范圍-以后再訪問最有可能不是您剛剛創建的特定格式( FormatConditions(3)
  2. 您輸入的公式是默認的英語公式-由於某些原因, FormatConditions.Add需要使用本地公式。

我重新編寫了您的代碼,看看它是否可以解決您的問題:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM