Is there a limit to the number of conditional formatting rules?

288enzo

Well-known Member
Joined
Feb 8, 2009
Messages
727
Office Version
  1. 2016
Platform
  1. Windows
Everything was working fine until I added my 11th rule.

Now, on my 11th rule I'm getting Subscript out of range, Run-time error '9':

When I run this by itself, I have no troubles.
VBA Code:
Sub anothertest()
    With Range(Cells(2, 14), Cells(574, 14))
        .FormatConditions.Add Type:=xlExpression, Formula1:="=RIGHT(RC14,5)=""To Go"""
        With .FormatConditions(1)
            .Interior.Color = RGB(255, 192, 0) 'ORANGE
            .StopIfTrue = False
        End With
    End With
End Sub

When I incorporate it into the following, that's when I get the out of range error.
VBA Code:
    With Cells
   
        .FormatConditions.Delete
        .FormatConditions.Add Type:=xlTextString, String:="N/A", TextOperator:=xlContains
        With .FormatConditions(1)
            .Interior.Color = RGB(0, 0, 0) 'BLACK
            .StopIfTrue = False
        End With
        .FormatConditions.Add Type:=xlTextString, String:="Complete", TextOperator:=xlBeginsWith
        With .FormatConditions(2)
            .Interior.Color = RGB(0, 176, 80) 'GREEN
            .StopIfTrue = False
        End With
        .FormatConditions.Add Type:=xlTextString, String:="Registered", TextOperator:=xlContains
        With .FormatConditions(3)
            .Interior.Color = RGB(255, 255, 0) 'YELLOW
            .StopIfTrue = False
        End With
        .FormatConditions.Add Type:=xlTextString, String:="Needs*", TextOperator:=xlContains
        With .FormatConditions(4)
            .Interior.Color = RGB(255, 0, 0) 'RED
            .StopIfTrue = False
        End With
        .FormatConditions.Add Type:=xlTextString, String:="*Remaining", TextOperator:=xlContains
        With .FormatConditions(5)
            .Interior.Color = RGB(255, 0, 0) 'RED
            .StopIfTrue = False
        End With
        .FormatConditions.Add Type:=xlTextString, String:="Required", TextOperator:=xlContains
        With .FormatConditions(6)
            .Interior.Color = RGB(255, 0, 0) 'RED
            .StopIfTrue = False
        End With
        .FormatConditions.Add Type:=xlTextString, String:="Test", TextOperator:=xlContains
        With .FormatConditions(7)
            .Interior.Color = RGB(112, 48, 160) 'PURPLE
            .StopIfTrue = False
        End With
        .FormatConditions.Add Type:=xlTextString, String:="Up to Date", TextOperator:=xlContains
        With .FormatConditions(8)
            .Interior.Color = RGB(0, 176, 80) 'GREEN
            .StopIfTrue = False
        End With

    End With
   
    With Range(Cells(2, 18), Cells(LR, 18))
        .FormatConditions.Add Type:=xlTextString, String:="Yes", TextOperator:=xlContains
        With .FormatConditions(9)
            .Interior.Color = RGB(0, 176, 80) 'GREEN
            .StopIfTrue = False
        End With
        .FormatConditions.Add Type:=xlTextString, String:="No", TextOperator:=xlContains
        With .FormatConditions(10)
            .Interior.Color = RGB(255, 0, 0) 'RED
            .StopIfTrue = False
        End With
    End With
   
    With Range(Cells(2, 14), Cells(LR, 14))
        .FormatConditions.Add Type:=xlExpression, Formula1:="=RIGHT(RC14,5)=""To Go"""
        With .FormatConditions(11)
            .Interior.Color = RGB(255, 192, 0) 'ORANGE
            .StopIfTrue = False
        End With
    End With
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
You are getting that error because you are trying to reference a format condition that does not exist. A format condition collection is a range property. You have to start at 1 again when you add a new format condition for a new range w/o any previously defined format conditions, and With Range(Cells(2, 18), Cells(LR, 18)) is a new range.
 
Upvote 0
You are better off using a variable when adding a format condition. That way you don't need to keep track of the collection index numbers when adding multiple format conditions.

VBA Code:
Dim FC As FormatCondition

    With Cells
        .FormatConditions.Delete
        
        Set FC = .FormatConditions.Add(Type:=xlTextString, String:="N/A", TextOperator:=xlContains)
        With FC
            .Interior.Color = RGB(0, 0, 0)            'BLACK
            .StopIfTrue = False
        End With
        Set FC = Nothing
 
Upvote 0
The simplest way (IMO) is to do it like
VBA Code:
    With Cells
   
        .FormatConditions.Delete
        .FormatConditions.Add Type:=xlTextString, String:="N/A", TextOperator:=xlContains
        With .FormatConditions(.FormatConditions.Count)
            .Interior.Color = RGB(0, 0, 0) 'BLACK
            .StopIfTrue = False
        End With
 
Upvote 0
Solution
The simplest way (IMO) is to do it like
VBA Code:
    With Cells
  
        .FormatConditions.Delete
        .FormatConditions.Add Type:=xlTextString, String:="N/A", TextOperator:=xlContains
        With .FormatConditions(.FormatConditions.Count)
            .Interior.Color = RGB(0, 0, 0) 'BLACK
            .StopIfTrue = False
[QUOTE="rlv01, post: 5930103, member: 403997"]
You are better off using a variable when adding a format condition. That way you don't need to keep track of the collection index numbers when adding multiple format conditions.

[CODE=vba]Dim FC As FormatCondition

    With Cells
        .FormatConditions.Delete
       
        Set FC = .FormatConditions.Add(Type:=xlTextString, String:="N/A", TextOperator:=xlContains)
        With FC
            .Interior.Color = RGB(0, 0, 0)            'BLACK
            .StopIfTrue = False
        End With
        Set FC = Nothing

End With

[/CODE]
[/QUOTE]
Thank you both, I ultimately chose Fluff's answer as it was the simplest :-)

On a side note, I did try to change the last conditional formatting "To Go" to a 1 because it was a new range, but that didn't work. It basiclly changed all "N/A" cells to orange, which "N/A" was also number 1.
 
Upvote 0
Glad we could help & thanks for the feedback.
I did try to change the last conditional formatting "To Go" to a 1
On this occasion you would need to make it 9 as there are already 8 rules for those cells.
 
Upvote 0

Forum statistics

Threads
1,224,822
Messages
6,181,165
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top