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

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.

Zot

Well-known Member
Joined
Nov 26, 2020
Messages
1,867
Office Version
  1. 2016
Platform
  1. Windows
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.
 

cmowla

Board Regular
Joined
Sep 21, 2021
Messages
243
Office Version
  1. 365
Platform
  1. Windows
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
 

Forum statistics

Threads
1,147,745
Messages
5,742,954
Members
423,767
Latest member
dafydd_jwc

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
Top