• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

How To Count Colored Cells

Sidharth Sarkar

New Member
I am trying to convert the below sub to function but i am not getting the correct result on function.
the SUB works fine and returns correct value but function somewhere its lacking at the if condition, can someone help

>>> use code - tags <<<
Code:
Sub DisplayColorCount()
    Dim Rng As Range
    Dim CountRange As Range
    Dim ColorRange As Range
    Dim xBackColor As Long
    On Error Resume Next
    xTitleId = "dptutorials"
    Set CountRange = Application.Selection
    Set CountRange = Application.InputBox("Count Range :", xTitleId, CountRange.Address, Type:=8)
    Set ColorRange = Application.InputBox("Color Range(single cell):", xTitleId, Type:=8)
    Set ColorRange = ColorRange.Range("A1")
    xReturn = 0
    For Each Rng In CountRange
        qqq = Rng.Value
        xxx = Rng.DisplayFormat.Interior.Color
        If Rng.DisplayFormat.Interior.Color = ColorRange.DisplayFormat.Interior.Color Then
            xBackColor = xBackColor + 1
        End If
    Next
    MsgBox "Count of Colors is " & xBackColor
End Sub

----------------------------------------------------------------------------------------------------------------------------


Function DisplayColorCount(Color_Rng As Range, Count_Rng As Range) As Long
    Dim Rng As Range
    Dim CountRange As Range
    Dim ColorRange As Range
    Dim xBackColor As Long
    On Error Resume Next
  
    Set CountRange = Color_Rng
    Set ColorRange = Count_Rng
  
  
    For Each Rng In CountRange
        If Rng.DisplayFormat.Interior.Color = ColorRange.DisplayFormat.Interior.Color Then
            xBackColor = xBackColor + 1
        End If
    Next
    DisplayColorCount = xBackColor
End Function
 
Last edited by a moderator:
Sidharth Sarkar
Did You skip few sentences from Forum Rules?
Please post, new posts in the correct forums,
This seems to belong to VBA Macros
In this time, this thread is moved to correct Forum.
 
Back
Top