Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Count colored cells with VBA
#1
Example:

Sub CountColoredCells()
    Dim countColor As Long
    Dim cell As Range
    Dim targetColor As Long

    'Specify the target color (RGB value) to count
    targetColor = RGB(255, 0, 0) 'Red color in this example

    countColor = 0 'Initialize the counter

    'Loop through each cell in the range
    For Each cell In Range("A1:A10")
        'Check if the cell's background color matches the target color
        If cell.Interior.Color = targetColor Then
            countColor = countColor + 1 'Increment the counter
        End If
    Next cell

    MsgBox "Number of cells with the target color: " & countColor
End Sub

Explanation:

The code defines a subroutine named CountColoredCells.
It declares variables for the counter (countColor), a loop variable (cell), and the target color (targetColor) you want to count. 
In this example, we use the RGB value for the color red (255, 0, 0).
The counter countColor is initialized to 0.
The For Each loop is used to loop through each cell in the range "A1:A10".
The If statement checks if the background color of the current cell matches the target color (targetColor = RGB(255, 0, 0)). 
If there is a match, the counter countColor is incremented by 1.
Finally, a message box displays the number of cells with the target color.
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 772 07-27-2023, 11:26 AM
Last Post: Qomplainerz
  Split text in cells with VBA Qomplainerz 0 412 07-27-2023, 11:09 AM
Last Post: Qomplainerz
  Calculate the avg and color the cells when their values are above or below the avg Qomplainerz 0 425 07-27-2023, 10:41 AM
Last Post: Qomplainerz
  Protect specific cells with VBA Qomplainerz 0 378 07-27-2023, 10:39 AM
Last Post: Qomplainerz
  Format cells with conditional formatting in VBA Qomplainerz 0 353 07-27-2023, 08:16 AM
Last Post: Qomplainerz
  Loop to fill numbers in cells with VBA Qomplainerz 0 375 07-27-2023, 08:16 AM
Last Post: Qomplainerz
  Count the number of rows in a worksheet with VBA Qomplainerz 0 319 07-27-2023, 07:50 AM
Last Post: Qomplainerz
  Formatting cells with VBA Qomplainerz 0 324 07-27-2023, 06:31 AM
Last Post: Qomplainerz

Forum Jump:


Users browsing this thread: 6 Guest(s)