+ Reply to Thread
Results 1 to 5 of 5

Count all colors in a range

Hybrid View

  1. #1
    Registered User
    Join Date
    08-29-2012
    Location
    Oakville, Ontario
    MS-Off Ver
    Excel 2016, 2019. 365 Online
    Posts
    15

    Count all colors in a range

    This is new to me and I am losing my mind with counting colors. I can count one color in a range. I would like to be able to count all the colors (in cells) in a specified range. VBA is new to me and I have exhausted myself (even with the excellent examples provided) trying to figure out how to count multiple colors in a single row (or range)
    I have attached my spreadsheet to make it easier and I am using the UDF colorfunction... am I on the right track? Is this an easy solution?

    Please help.KAP Sawmill Working at Height Improvement Schedule.xlsm

  2. #2
    Valued Forum Contributor
    Join Date
    03-29-2013
    Location
    United Kingdom
    MS-Off Ver
    Office/Excel 2013
    Posts
    1,749

    Re: Count all colors in a range

    I had a few problems getting the function to work properly so i adjusted it

    Function ColorFunction(rColor As Range, rRange As Range)
        Dim rCell As Range
        Dim lCol As Long
        Dim vResult
         
         ''''''''''''''''''''''''''''''''''''''
         'Written by Ozgrid Business Applications
         'www.ozgrid.com
         
         'Sums or counts cells based on a specified fill color.
         '''''''''''''''''''''''''''''''''''''''
         
        lCol = rColor.Interior.ColorIndex
         
            For Each rCell In rRange
                If rCell.Interior.ColorIndex = lCol Then
                    vResult = vResult + 1
                End If
            Next rCell
         
        ColorFunction = vResult
    End Function
    Last edited by AndyLitch; 05-06-2013 at 02:02 PM.
    Elegant Simplicity............. Not Always

  3. #3
    Registered User
    Join Date
    08-29-2012
    Location
    Oakville, Ontario
    MS-Off Ver
    Excel 2016, 2019. 365 Online
    Posts
    15

    Re: Count all colors in a range

    Thank you Andy, I had no problem getting the function to work with one color, and I see in the code that this is for a specified color. BUT what if I want to count for multiple colors? Is it a different piece of code? I know next to nothing about VBA and appreciate any and all help.
    Can this function be written as a nested formula? And if so what would the syntax look like?

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Count all colors in a range

    Hello NotQuiteThere,

    This UDF will count all colored cells in a the specified range. Also, you don't need to use F( to recalculate if a value cells is changed.
    Function ColoredCellCount(ByRef Rng As Range) As Long
    
        Dim Cell As Range
        Dim cntColor As Long
        
        Application.Volatile
        
            For Each Cell In Rng
                If Cell.Interior.ColorIndex <> xlColorIndexNone Then
                    cntColor = cntColor + 1
                End If
            Next Cell
            
            ColoredCellCount = cntColor
            
    End Function
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  5. #5
    Registered User
    Join Date
    08-29-2012
    Location
    Oakville, Ontario
    MS-Off Ver
    Excel 2016, 2019. 365 Online
    Posts
    15

    Re: Count all colors in a range

    Thank you, from what I can see that has met my needs. I will confirm the solution and then figure out how to mark the thread solved.

    ** I have had an opportunity to check the function against my data and it works like a charm. Thank you for marking the post solved and thank you very much for such a quick and accurate solution Leith. **
    Last edited by NotQuiteThere; 05-06-2013 at 10:05 PM. Reason: Added confirmation of the solution.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1