Web design

Closed
renea - 25 May 2010 à 10:44
rizvisa1 Posts 4478 Registration date Thursday 28 January 2010 Status Contributor Last seen 5 May 2022 - 25 May 2010 à 16:12
Hello,
I need an excel formula that will count the number of cells that are highlighted black and contain data.
Example - 5 cells highlighted black, 4 cells with data, 6 cells blank. Formula should return 9 total cells.

Thanks!


1 response

rizvisa1 Posts 4478 Registration date Thursday 28 January 2010 Status Contributor Last seen 5 May 2022 766
25 May 2010 à 16:02
1 Press ALT + F11 to start VBE
2 Click on Insert to add a module
3 Paste the code (after instructions)
4 Call the function as

a. To Find count of all cells in range ,F1:F6 with red color
=CountOnColor(255,False,F1:F6)

a. To Find count of all cells in range ,F1:F6 with red color and with data
=CountOnColor(255,true,F1:F6)

C. To Find count of all cells in range ,F1:F6 with red color and no data
=CountOnColor(255,false,F1:F6) - CountOnColor(255,true,F1:F6)


Function CountOnColor(backGroundColorRGB As Long, CountOnlyNonBlank As Boolean, ParamArray OtherArgs()) As Variant
Dim Count As Double
Dim temp As Variant
Dim Cell As Range
    
    Application.Volatile
    
    On Error GoTo 0

    Count = 0
    If (UBound(OtherArgs) < 0) Then
        MsgBox "Not Sufficient parameters Passed"
        CountOnColor = "** ERROR **"
        Exit Function
    End If
    
    For Each items In OtherArgs
        
        temp = 0
        
        Select Case LCase(TypeName(items))
            
            Case Is = "range"
                
                For Each Cell In items
                
                    If Cell.Interior.Color = backGroundColorRGB Then
                        
                        temp = temp + 1
                        If ((CountOnlyNonBlank) And (Cell = "")) Then temp = temp - 1
                    
                    End If
                Next Cell
                
            Case Else
            
                GoTo NON_CELL_ERROR
                
        End Select
        
        Count = Count + temp
            
    Next items

    CountOnColor = Count
    Exit Function
    
NON_CELL_ERROR:
    MsgBox ("Non Cell/Range parameter encountered")
    CountOnColor = "** ERROR **"
    Exit Function
End Function
Ambucias Posts 47312 Registration date Monday 1 February 2010 Status Moderator Last seen 28 March 2026 11,166
25 May 2010 à 16:05
Hello Rizvisa1
I have been observing you with Excel messages. You work wonders don't you!
Thank you
rizvisa1 Posts 4478 Registration date Thursday 28 January 2010 Status Contributor Last seen 5 May 2022 766
25 May 2010 à 16:12
Thank you.Ambucias. We all learn from each others. I have picked up a trick or two looking at other people code too.