help

cmccabe

Active Member
Joined
Feb 20, 2008
Messages
396
Hello,

When I use the code below I get there are 1 cells with the red background color. The original cell range has A1:A4 red so why is the code not returning 4? Thanks.

Code:
Sub CountCells()
    ' Count Cells with content or with background coloring
    Dim c As Range, Cnt As Integer
    Cnt = 0
        For Each c In Range(Cells(ActiveCell.Row, 1), Cells(ActiveCell.Row, 256))
        If c.Interior.ColorIndex = 3 Then Cnt = Cnt + 1
    Next c
         MsgBox "There are " & Cnt & " cells that have a red background color"
End Sub
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
The macro loops through all the cells in one row. A1:A4 are in a column.

This loops through all cells in the activecell row.
Code:
For Each c In Range(Cells(ActiveCell.Row, 1), Cells(ActiveCell.Row, 256))
 
Upvote 0
Code:
For Each c In Range(Cells(firstrow,ActiveCell.Column), Cells(lastrow, ActiveCell.Column))

where firstrow and lastrow correspond to the first and last rows you wish to loop through (ie substitute numbers or your numeric variables)
 
Upvote 0
That works great, thank you.

If I also wanted to count the green cells and yellow cells in addition to the red is that possible? Thanks.
 
Upvote 0

Forum statistics

Threads
1,203,502
Messages
6,055,769
Members
444,822
Latest member
Hombre

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
Back
Top