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

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).

Zot

Well-known Member
Joined
Nov 26, 2020
Messages
1,758
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,143,637
Messages
5,719,961
Members
422,251
Latest member
bonebreaker100

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