Counting Cells based on Formatting

Pavertheway

New Member
Joined
Nov 5, 2018
Messages
47
Hi,

I use this code below to count cells based on their colour, which was coloured via conditional formatting:

Code:
Sub SumCountByConditionalFormat()    Dim indRefColor As Long
    Dim cellCurrent As Range
    Dim cntRes As Long
    Dim sumRes
    Dim cntCells As Long
    Dim indCurCell As Long
 
    cntRes = 0
    sumRes = 0
 
    cntCells = Selection.CountLarge
    indRefColor = ActiveCell.DisplayFormat.Interior.Color
 
    For indCurCell = 1 To (cntCells - 1)
        If indRefColor = Selection(indCurCell).DisplayFormat.Interior.Color Then
            cntRes = cntRes + 1
            sumRes = WorksheetFunction.SUM(Selection(indCurCell), sumRes)
        End If
    Next
   MsgBox "Count=" & cntRes & vbCrLf & "Sum= " & sumRes & vbCrLf & vbCrLf & _
        "Color=" & Left("000000", 6 - Len(Hex(indRefColor))) & _
        Hex(indRefColor) & vbCrLf, , "Count & Sum by Conditional Format color"
End Sub

This works well, however I was wondering if I could automate the process - now I have to select my cell range (B3:T47), then select an active cell (Y1) for it to work - and tweaking with the cntCells section I just seem to stop it working correctly!

Any help would be greatly appreciated.

Thanks,

Adam
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Try:
Code:
indRefColor = Range("Y1").DisplayFormat.Interior.Color
This is a direct reference to Y1 so you don't have to select it.
 
Upvote 0
Hi,

thanks for your reply - I tried that and for some reason it just doesn't calculate properly.
When I run the Macro with manual input, it gives the count at 132 cells being the colour selected (which is correct).

When I run the Macro with the code you suggested, it gives 669 cells.
 
Upvote 0

Forum statistics

Threads
1,214,959
Messages
6,122,476
Members
449,087
Latest member
RExcelSearch

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