Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Format cells with conditional formatting in VBA
#1
Example:

Sub ApplyConditionalFormatting()
    Dim rng As Range
   
    'Specify the range where conditional formatting should be applied
    Set rng = Range("B2:B10")
   
    'Apply conditional formatting based on cell values
    With rng.FormatConditions.Add(Type:=xlCellValue, Operator:=xlBetween, Formula1:="5", Formula2:="8")
        .Interior.Color = RGB(255, 255, 0) 'Yellow color
    End With
End Sub

Explanation:

The code defines a subroutine named ApplyConditionalFormatting.
It declares a variable rng to store the range of cells where conditional formatting should be applied.
The Set statement is used to specify the range B2 to B10 as the range where conditional formatting will be applied.
The With statement is used to work with the FormatConditions object of the specified range.
The Add method is used to add a new conditional formatting rule.
The conditional formatting is based on cell values between 5 and 8 (inclusive). If a cell value falls within this range, the formatting inside the With block will be applied.
The interior color of the cells is set to yellow using the RGB function.
Also follow me on Youtube for videos about video games:
https://www.youtube.com/channel/UCxfkGVU...2mQ/videos
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Format cells in a range with bold font and yellow background color Qomplainerz 0 344 07-27-2023, 11:26 AM
Last Post: Qomplainerz
  Split text in cells with VBA Qomplainerz 0 269 07-27-2023, 11:09 AM
Last Post: Qomplainerz
  Count colored cells with VBA Qomplainerz 0 226 07-27-2023, 11:07 AM
Last Post: Qomplainerz
  Calculate the avg and color the cells when their values are above or below the avg Qomplainerz 0 270 07-27-2023, 10:41 AM
Last Post: Qomplainerz
  Protect specific cells with VBA Qomplainerz 0 256 07-27-2023, 10:39 AM
Last Post: Qomplainerz
  Loop to fill numbers in cells with VBA Qomplainerz 0 228 07-27-2023, 08:16 AM
Last Post: Qomplainerz
  Formatting cells with VBA Qomplainerz 0 215 07-27-2023, 06:31 AM
Last Post: Qomplainerz
  How to format an entire column as number without thousand separator in Excel VBA Qomplainerz 0 350 04-13-2023, 12:26 PM
Last Post: Qomplainerz
  How to format an entire column as number with thousand separator in Excel VBA Qomplainerz 0 324 04-13-2023, 12:25 PM
Last Post: Qomplainerz

Forum Jump:


Users browsing this thread: 1 Guest(s)