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
 

Some videos you may like

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.

mumps

Well-known Member
Joined
Apr 11, 2012
Messages
10,152
Try:
Code:
indRefColor = Range("Y1").DisplayFormat.Interior.Color
This is a direct reference to Y1 so you don't have to select it.
 

Pavertheway

New Member
Joined
Nov 5, 2018
Messages
47
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.
 

Watch MrExcel Video

Forum statistics

Threads
1,123,078
Messages
5,599,632
Members
414,326
Latest member
kfg1287

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
Top