+ Reply to Thread
Results 1 to 12 of 12

How to count background color

  1. #1
    Forum Contributor
    Join Date
    03-03-2004
    Location
    mumbai
    MS-Off Ver
    2003,2007
    Posts
    151

    How to count background color

    Hi,
    Is there a way to count the background color from a range of cell?
    for example: In a range of A1:B5 there are 5 cells highlighted with yellow background, the formula should give me 5 counts for yellow background.
    thank you

  2. #2
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: How to count background color

    A simple UDF can count all cells with a colored background:
    Please Login or Register  to view this content.
    Use as:
    Formula: copy to clipboard
    Please Login or Register  to view this content.
    let Source = #table({"Question","Thread", "User"},{{"Answered","Mark Solved", "Add Reputation"}}) in Source

    If I give you Power Query (Get & Transform Data) code, and you don't know what to do with it, then CLICK HERE

    Walking the tightrope between genius and eejit...

  3. #3
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: How to count background color

    Extending that a little allows us to count cells with a specific color fill:
    Please Login or Register  to view this content.
    Use as:
    Formula: copy to clipboard
    Please Login or Register  to view this content.

  4. #4
    Forum Contributor
    Join Date
    03-03-2004
    Location
    mumbai
    MS-Off Ver
    2003,2007
    Posts
    151

    Re: How to count background color

    Hi,
    I have used the above macro provided by Olly as well as the below code
    Function ColorFunction(rColor As Range, rRange As Range, Optional SUM As Boolean)
    Dim rCell As Range
    Dim lCol As Long
    Dim vResult
    lCol = rColor.Interior.ColorIndex
    If SUM = True Then
    For Each rCell In rRange
    If rCell.Interior.ColorIndex = lCol Then
    vResult = WorksheetFunction.SUM(rCell, vResult)
    End If
    Next rCell
    Else
    For Each rCell In rRange
    If rCell.Interior.ColorIndex = lCol Then
    vResult = 1 + vResult
    End If
    Next rCell
    End If
    ColorFunction = vResult
    End Function

    However, whenever there is a change in background color it gives me #NAME? error.
    Does anyone know the possible reason?

  5. #5
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: How to count background color

    It's not clear which function you have used, or how, so hard to advise why you have the #NAME error. Can you attach your workbook? If not, can you clearly explain which funtion you are using, and where.

  6. #6
    Forum Contributor
    Join Date
    03-03-2004
    Location
    mumbai
    MS-Off Ver
    2003,2007
    Posts
    151

    Re: How to count background color

    Its in built function generated by the macro code pasted above and function name is =ColorFunction.

  7. #7
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: How to count background color

    Not specific enough to help you. This doesn't tell me where you have tried to use this function and which arguments you are passing to it.

    Please attach a workbook.

    Did you try using the function and formula I provided in Post #2?

  8. #8
    Forum Contributor
    Join Date
    03-03-2004
    Location
    mumbai
    MS-Off Ver
    2003,2007
    Posts
    151

    Re: How to count background color

    yes, I did, but same error is popping up the moment i close and open the file #NAME? error.

  9. #9
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: How to count background color

    Please attach your workbook.

  10. #10
    Forum Contributor
    Join Date
    03-03-2004
    Location
    mumbai
    MS-Off Ver
    2003,2007
    Posts
    151

    Re: How to count background color

    Sorry, forgot to attach. Please find the attached. The moment i close and open the file it is giving me the error.
    Attached Files Attached Files

  11. #11
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: How to count background color

    Works perfectly for me. Have you made sure that macros are enabled for this file? Then try pressing F9 to calculate the worksheet.

  12. #12
    Forum Contributor
    Join Date
    03-03-2004
    Location
    mumbai
    MS-Off Ver
    2003,2007
    Posts
    151

    Re: How to count background color

    Yes, It is working fine. Sorry to bother you i had not selected enables macro for this file. Thank you so much for your help.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Count Cell with Background color
    By rizmomin in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 07-16-2013, 05:29 AM
  2. VBA Module counts background color, would like it to count font color
    By LawBeforeGrace in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-25-2013, 08:11 AM
  3. Count if Same Text name AND Same Background Color Yellow
    By ORAM in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 09-10-2012, 12:15 PM
  4. [SOLVED] How do you count cells with background color yellow?
    By Stephanie D in forum Excel General
    Replies: 6
    Last Post: 10-18-2005, 12:05 AM
  5. [SOLVED] Need help with complicated use of count background color of Cell
    By Jane in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 08-08-2005, 12:05 PM

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