VBA for conditional formatting using three colors

declanGISS

New Member
Joined
Sep 21, 2021
Messages
1
Office Version
  1. 365
Platform
  1. Windows
I was wondering if there is a simple VBA to conditional format a large number of cells based on the cell values.

Essentially I would want any cell less than 2 to be red, cells from 2-4 could be orange and cells greater than 4 would be red. I am sure it is relatively easy, I am just drawing a blank right now.
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
It can be done but you must state the condition how you'd want it to happen.

Does it involve specific range of cells?
Does the color change after entering number?

Need to explain what steps took place to get the condition. I'm sure many can help you with that. :)
Providing sheet capture would help too.
 
Upvote 0
This should work:
VBA Code:
Sub Conditional_Formatting_Test()

Dim sheetNameWhereRangeToBeFormattedIs As String
sheetNameWhereRangeToBeFormattedIs = ActiveSheet.name

Dim formatted_Range As Range
Set formatted_Range = Sheets(sheetNameWhereRangeToBeFormattedIs).Range("A1:G10")

'First delete the formatting conditions for the range to start fresh.
formatted_Range.FormatConditions.Delete

'Now create the formatting rules.
With formatted_Range
    .FormatConditions.Add(Type:=xlCellValue, operator:=xlLess, Formula1:="2").Interior.color = RGB(255, 0, 0) 'red
    .FormatConditions.Add(Type:=xlCellValue, operator:=xlBetween, Formula1:="2", Formula2:=4).Interior.color = RGB(255, 165, 0) 'orange
    .FormatConditions.Add(Type:=xlCellValue, operator:=xlGreater, Formula1:="4").Interior.color = RGB(255, 0, 0) 'red
End With

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,646
Messages
6,120,715
Members
448,985
Latest member
chocbudda

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