VBA Conditional Format Error 9

mattadams84

New Member
Joined
Oct 30, 2016
Messages
36
Hi,

I am trying to apply conditional formatting using VBA. I have got this code to work successfully :

Code:
Sub ResetCF()Application.ScreenUpdating = False
    With ActiveSheet
    
          With .Range("$E:$R")
            'Blank Cells
            .FormatConditions.Add xlExpression, Formula1:="=$E1="""""
            .FormatConditions(1).Interior.ColorIndex = 2
            .FormatConditions(1).StopIfTrue = False
        End With
        
         With .Range("$E:$E,$G:$G,$I:$I,$k:$k,$m:$m,$o:$o,$q:$q,$s:$s")
            'Borders
            .FormatConditions.Add xlExpression, Formula1:="=$E1<>"""""
            With .FormatConditions(2).Borders(xlLeft)
                 .LineStyle = xlContinuous
                 .ColorIndex = xlAutomatic
                 .TintAndShade = 0
                 .Weight = xlThin
                End With
            .FormatConditions(2).StopIfTrue = False
        End With
        
        With .Range("$G:$G")
            'green
            .FormatConditions.Add xlExpression, Formula1:="=$G1<$E1-2"
            .FormatConditions(3).Interior.Color = RGB(198, 239, 206)
            .FormatConditions(3).Font.Color = RGB(0, 97, 0)
            .FormatConditions(3).StopIfTrue = False
        End With
        
        With .Range("$G:$G")
            'red
            .FormatConditions.Add xlExpression, Formula1:="=$G1>$E1+2"
            .FormatConditions(4).Interior.Color = RGB(255, 199, 206)
            .FormatConditions(4).Font.Color = RGB(156, 0, 6)
            .FormatConditions(4).StopIfTrue = False
        End With
        
        With .Range("$G:$G")
            'yellow
            .FormatConditions.Add xlExpression, Formula1:="=OR($G1<$E1+2,$G1>$E1-2)"
            .FormatConditions(5).Interior.Color = RGB(255, 235, 156)
            .FormatConditions(5).Font.Color = RGB(156, 101, 0)
            .FormatConditions(5).StopIfTrue = False
        End With
        
        
    End With
Application.ScreenUpdating = True
End Sub
The problem i am having is that i now want to add the same conditional formatting (red,green,yellow) to column H but that uses a different formula. So what i did was add this :

Code:
With .Range("$H:$H")            
            'green
            .FormatConditions.Add xlExpression, Formula1:="=$H1<$F1-2"
            .FormatConditions(6).Interior.Color = RGB(198, 239, 206)
            .FormatConditions(6).Font.Color = RGB(0, 97, 0)
            .FormatConditions(6).StopIfTrue = False
        End With
        
        With .Range("$H:$H")
            'red
            .FormatConditions.Add xlExpression, Formula1:="=$H1>$F1+2"
            .FormatConditions(7).Interior.Color = RGB(255, 199, 206)
            .FormatConditions(7).Font.Color = RGB(156, 0, 6)
            .FormatConditions(7).StopIfTrue = False
        End With
        
        With .Range("$H:$H")
            'yellow
            .FormatConditions.Add xlExpression, Formula1:="=OR($H1<$F1+2,$H1>$F1-2)"
            .FormatConditions(8).Interior.Color = RGB(255, 235, 156)
            .FormatConditions(8).Font.Color = RGB(156, 101, 0)
            .FormatConditions(8).StopIfTrue = False
        End With
The problem i am getting is on this line :

Code:
.FormatConditions(6).Interior.Color = RGB(198, 239, 206)
I get an error 9 message saying subscript is out of range. I guess it is because the "Look" of the conditional formatting is the same for FormatCondtions(3). Is there any way around this? Perhaps i could define the look somewhere and then apply it to different columns bearing in mind that each column will have a different formula...
 
Last edited:

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
75,362
Office Version
365
Platform
Windows
Are you sure you already have 5 conditional formatting rules applied to column H?

Does this work?
Code:
    With .Range("$H:$H").FormatConditions
    
        .Delete ' optional
        'green
        .Add xlExpression, Formula1:="=$H1<$F1-2"
        With .Item(.Count)
            .Interior.Color = RGB(198, 239, 206)
            .Font.Color = RGB(0, 97, 0)
            .StopIfTrue = False
        End With
        
        'red
        .Add xlExpression, Formula1:="=$H1>$F1+2"
        With .Item(.Count)
            .Interior.Color = RGB(255, 199, 206)
            .Font.Color = RGB(156, 0, 6)
            .StopIfTrue = False
        End With
        
        .Add xlExpression, Formula1:="=OR($H1<$F1+2,$H1>$F1-2)"
        With .Item(.Count)
            .Interior.Color = RGB(255, 235, 156)
            .Font.Color = RGB(156, 101, 0)
            .StopIfTrue = False
        End With
        
    End With
 

Forum statistics

Threads
1,082,114
Messages
5,363,244
Members
400,722
Latest member
DrewPop24

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top