Conditional Formatting Macro

carrieebacon

New Member
Joined
Jan 15, 2024
Messages
49
Office Version
  1. 365
Platform
  1. Windows
I am trying to apply the following conditional formatting to a macro. It is creating the formulas and applying them to the correct range, but it is only setting the format for the first formula. Any ideas?

This is my code:
Sub CondishFormatMulti()
Dim MyRange As Range
Set MyRange = Range("$E:$E, $I:$I, $M:$M, $Q:$Q")
MyRange.FormatConditions.Delete
MyRange.FormatConditions.Add Type:=xlCellValue, Operator:=xlBetween, _
Formula1:="=0.1", Formula2:="=0.5"
MyRange.FormatConditions(1).Interior.Color = RGB(255, 230, 153)
MyRange.FormatConditions(1).Font.Color = RGB(128, 96, 0)
MyRange.FormatConditions.Add Type:=xlCellValue, Operator:=xlBetween, _
Formula1:="=-0.1", Formula2:="=-0.5"
MyRange.FormatConditions(1).Interior.Color = RGB(255, 230, 153)
MyRange.FormatConditions(1).Font.Color = RGB(128, 96, 0)
MyRange.FormatConditions.Add Type:=xlCellValue, Operator:=xlBetween, _
Formula1:="=0.001", Formula2:="=0.1"
MyRange.FormatConditions(1).Interior.Color = RGB(198, 224, 180)
MyRange.FormatConditions(1).Font.Color = RGB(55, 86, 35)
MyRange.FormatConditions.Add Type:=xlCellValue, Operator:=xlBetween, _
Formula1:="=-0.001", Formula2:="=-0.1"
MyRange.FormatConditions(1).Interior.Color = RGB(198, 224, 180)
MyRange.FormatConditions(1).Font.Color = RGB(55, 86, 35)
MyRange.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, _
Formula1:="=0.5"
MyRange.FormatConditions(1).Interior.Color = RGB(255, 0, 0)
MyRange.FormatConditions(1).Font.Color = RGB(0, 0, 0)
MyRange.FormatConditions.Add Type:=xlCellValue, Operator:=xlLess, _
Formula1:="=-0.5"
MyRange.FormatConditions(1).Interior.Color = RGB(255, 0, 0)
MyRange.FormatConditions(1).Font.Color = RGB(0, 0, 0)
End Sub
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
but it is only setting the format for the first formula.
Because your code is written as
Rich (BB code):
MyRange.FormatConditions(1).
in each case. Try writing each condition by incrementing the conditions: (2), (3) etc.
 
Upvote 0
It's because you are making all the changes to only the first format condition. Here is an alternative approach.
VBA Code:
Sub CondishFormatMulti()
    Dim MyRange As Range


    Set MyRange = Range("$E:$E, $I:$I, $M:$M, $Q:$Q")
    MyRange.FormatConditions.Delete

    With MyRange.FormatConditions
        With .Add(Type:=xlCellValue, Operator:=xlBetween, Formula1:="=0.1", Formula2:="=0.5")
            .Interior.Color = RGB(255, 230, 153)
            .Font.Color = RGB(128, 96, 0)
        End With

        With .Add(Type:=xlCellValue, Operator:=xlBetween, Formula1:="=-0.1", Formula2:="=-0.5")
            .Interior.Color = RGB(255, 230, 153)
            .Font.Color = RGB(128, 96, 0)
        End With

        With .Add(Type:=xlCellValue, Operator:=xlBetween, Formula1:="=0.001", Formula2:="=0.1")
            .Interior.Color = RGB(198, 224, 180)
            .Font.Color = RGB(55, 86, 35)
        End With

        With .Add(Type:=xlCellValue, Operator:=xlBetween, Formula1:="=-0.001", Formula2:="=-0.1")
            .Interior.Color = RGB(198, 224, 180)
            .Font.Color = RGB(55, 86, 35)
        End With

        With .Add(Type:=xlCellValue, Operator:=xlGreater, Formula1:="=0.5")
            .Interior.Color = RGB(255, 0, 0)
            .Font.Color = RGB(0, 0, 0)
        End With

        With .Add(Type:=xlCellValue, Operator:=xlLess, Formula1:="=-0.5")
            .Interior.Color = RGB(255, 0, 0)
            .Font.Color = RGB(0, 0, 0)
        End With
    End With
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,223,929
Messages
6,175,461
Members
452,645
Latest member
Tante

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