Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Change cell colors based on values with VBA
#1
Example:

Sub ColorCellsBasedOnValue()
    Dim cell As Range

    'Loop through each cell in column B
    For Each cell In Range("B1:B10")
        If cell.Value > 5 Then
            cell.Interior.Color = RGB(255, 0, 0) 'Red color for values greater than 5
        Else
            cell.Interior.Color = RGB(0, 255, 0) 'Green color for values less than or equal to 5
        End If
    Next cell
End Sub

Explanation:

The code defines a subroutine named ColorCellsBasedOnValue.
It declares a variable cell of type Range, which will be used to loop through each cell in column B.
The For Each loop iterates through each cell in the range Range("B1:B10").
The If statement checks the value of each cell. If the value is greater than 5, the background color of the cell is changed to red using the RGB function (255, 0, 0). Otherwise, if the value is less than or equal to 5, the background color is changed to green (0, 255, 0).
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
  Sum values based on criteria Qomplainerz 0 245 07-27-2023, 11:20 AM
Last Post: Qomplainerz
  Create a pivot table based on data in a worksheet with VBA Qomplainerz 0 276 07-27-2023, 09:48 AM
Last Post: Qomplainerz
  Delete rows based on a condition Qomplainerz 0 273 07-27-2023, 09:47 AM
Last Post: Qomplainerz
  Add data validation to a cell with VBA Qomplainerz 0 238 07-27-2023, 09:10 AM
Last Post: Qomplainerz
  Copy and paste values with VBA Qomplainerz 0 236 07-27-2023, 09:08 AM
Last Post: Qomplainerz
  Calculate the sum of values in a column with VBA Qomplainerz 0 248 07-27-2023, 08:18 AM
Last Post: Qomplainerz
  Running VBA code when a cell's value changes Qomplainerz 0 175 07-27-2023, 07:04 AM
Last Post: Qomplainerz
  How to select a particular cell in Excel VBA Qomplainerz 0 305 04-13-2023, 12:02 PM
Last Post: Qomplainerz

Forum Jump:


Users browsing this thread: 1 Guest(s)