• Skip to main content
  • Skip to primary sidebar
  • Skip to footer

Microsoft Excel Tips and Tricks - Computergaga

  • Home
  • Excel Online Courses
  • Excel Functions List
  • Free Downloads
  • Blog
  • Tutorials
  • About
Home / VBA / Count Cells by Colour – Excel VBA Function

Count Cells by Colour – Excel VBA Function

If you have ever tried to count cells by colour in Excel, you may have noticed that Excel does not contain a function to accomplish this.

We can find out how many cells by filtering the list by colour, and maybe use the AGGREGATE function to return the count. This is an option. But having to filter the list each time may not be good enough.

Because functions such as COUNTIF cannot count by cell colour, we will need to create our own custom function (also known as User Defined Functions or UDF’s) to get the job done.

Custom Function to Count Cells by Colour

  1. Open the Visual Basic Editor by pressing Alt +F11 or by clicking the Visual Basic button on the Developer tab.
  2. Insert a new module if necessary by clicking the Insert menu and then Module.
  3. Copy and paste the code below into the code window (you can put this into the current workbook or the Personal Macro Workbook for global use).
Function COUNTIFCOLOUR(Colour As Range, rng As Range) As Long
Dim NoCells As Long
Dim CellColour As Long
Dim rngCell As Range
CellColour = Colour.Interior.Color
For Each rngCell In rng
    If rngCell.Interior.Color = CellColour Then
        NoCells = NoCells + 1
    End If
Next
COUNTIFCOLOUR = NoCells
End Function

You can then use this function like any other function in Excel. The arguments do not appear like normal functions, but everything else is the same.

Custom function to count by colour in Excel

How does it work?

Colour and rng are set up as arguments for the function. So when a user selects a cell containing the colour they want to count, this is assigned to the CellColour variable.

The user will select the range of cells to use and this is assigned to rng.

A For Next loop is then used to loop through each cell of that range checking if it is the colour we are looking for. If so 1 is added to the NoCells variable. This is assigned to the function for returning when the loop finishes.

Want to Learn More?

Want to learn more Excel VBA? Check out the complete Excel VBA online course. You will see more examples of User Defined Functions and a whole lot more.

Watch the Video

Related Posts:

  • Sum formulas only in Excel
    Sum Formulas Only in Excel
  • Case sensitive XLOOKUP feature
    Case Sensitive Lookup with XLOOKUP in Excel
  • N Functions in Excel thumbnail
    N Function in Excel
  • IMAGE function in Excel
    Excel IMAGE Function - Insert Images from a Cell Value

Reader Interactions

Comments

  1. mohideen says

    24 August 2016 at 8:17 pm

    many thanks for your effort Alan

    Reply
  2. Tony says

    12 April 2017 at 12:35 pm

    Hi Alan,
    This is ideal, however how do I count the VALUE of the coloured cell, not just how many cells there are.
    This is doing my head in and I welcome any help.
    Thanks in advance,

    Reply
    • computergaga says

      12 April 2017 at 5:08 pm

      Ah I think you want to sum the cell values instead. Just change the line that currently says NoCells = NoCells + 1.
      Instead maybe create the variable as SumCells and do SumCells = SumCells + rngCell.value

      Reply
  3. Arzi says

    27 April 2017 at 9:37 am

    Hi Alan,
    thanks a lot for the help, i tried your inbuild function works well! thumps up!
    Now tricky part, it does not read cell having colors under conditional formatting. what do you advise?
    Thank again!

    Reply
    • computergaga says

      28 April 2017 at 8:10 am

      Thanks Arzi. Hopefully I can get that problem solved in a future update on this blog post.

      Reply
  4. MM says

    30 April 2017 at 5:56 am

    my countifcolour function does not show up, help please. Does it matter if I’m using mac?

    Reply
    • computergaga says

      2 May 2017 at 10:14 am

      I don’t use a Mac so may need advice from elsewhere, but there are issues with VBA and Macs.

      Reply
  5. Scott Martin says

    3 May 2017 at 7:39 pm

    Alan,
    You are a life saver. I appreciate you putting this knowledge out here for the rest of us to find and use.
    Thanks

    Reply
    • computergaga says

      3 May 2017 at 8:50 pm

      Your welcome Scott, thanks very much for the comments.

      Reply
  6. Bruce Douty says

    4 May 2017 at 10:02 pm

    Hello Alan,
    Were you able to solve the problem of the code not recognizing the conditional formatting that colored a cell? I have red (Conditional Formatting) cells I’d like to count in a row, across multiple columns.
    also, How can i automatically update the count when a Red cell has been removed or added? Presently I have to manually update the cell to obtain the correct count.
    Thanks much
    Bruce

    Reply
    • computergaga says

      9 May 2017 at 3:15 pm

      Unfortunately not Bruce. It is very complex and dependent upon what Conditional Formatting is in play sometimes impossible. For more information I hve a couple of useful links.
      This one is for ExtendExcel who have a great app call Kutools that can accomplish this – https://www.extendoffice.com/documents/excel/2651-excel-count-cells-by-color-conditional-formatting.html
      And for possibly the best description on the intricacies of counting and summing Conditionally Formatted cells you can check this link – http://www.cpearson.com/excel/CFColors.htmI conclusion there is no Conditional Formatting rule that cannot be replicated in a cell formula. So instead of counting coloured cells. I would repeat the condition in a cell formula such as COUNTIFS or SUMPRODUCT.

      Reply
  7. Karina says

    29 June 2017 at 2:21 pm

    Hi I don’t know what i am doing wrong but I have the function you wrote in everything i am doing it on excel but on mac. so everything is a little different I don’t know why is not working can you help?

    Reply
    • computergaga says

      3 July 2017 at 6:05 pm

      Hi Karina,
      Unfortunately I do not own a mac so cannot comment. I am under the impressions though of huge differences in the VBA area.
      Alan

      Reply
  8. Christian says

    26 July 2017 at 6:51 pm

    thank you, it works great, but I might be doing something wrong, every time I close it , and save it, when I open the file the function disappears and I have to do it all over again, I saved it under excel enable workbook and excel enable template and it is the same, do i need to develop a module every time I open this file ?

    Reply
    • computergaga says

      27 July 2017 at 6:22 am

      No, as long as the file is saved as a macro enabled file, the module should be there when the file is re-opened.

      Reply
    • TrueTab says

      16 December 2022 at 9:51 pm

      same problem, with me also brother.
      cannot find solution.
      cant understand,
      what want to do for this problem.

      Reply
  9. Tina says

    10 August 2017 at 6:29 am

    Hi Computergaga,
    I’ve tried and it won’t work on the colour that’s done with Conditional Formatting. Is there any solutions?
    Thank you so much for your help.
    Cheers,
    Tina

    Reply
    • steve chase says

      15 December 2017 at 10:00 am

      Hi Tina, did you manage to find a solution to this?

      Reply
  10. steve chase says

    15 December 2017 at 9:59 am

    can this work if the cells have been coloured with conditional formatting? i cant seem to get it working with it.

    Reply
    • computergaga says

      18 December 2017 at 8:31 am

      Very difficult Steve. This technique does not work with Conditionally formatted cells.
      Here is a in depth article from Chip Pearson on it with a solution – http://www.cpearson.com/excel/CFColors.htm

      Reply
  11. Alisha says

    29 August 2020 at 2:37 pm

    Thank you, I didn’t know about developer in Excel, your code counted what I wanted. I may opt to take online VBA course. I did find when I added or removed a colored cell the formula does not run to update count. I had to manually update: remove and add the colored cell location, then the ‘countifcolour’ code would run, recounting all colored cells. Your response to Bruce states this isn’t fixable with out using other other options outside of excel, is that correct?

    Reply
    • Alan Murray says

      29 August 2020 at 4:46 pm

      Hi Alisha,
      It has been a long time since I wrote that post, but I believe the function doesn’t run unless a cell is edited to trigger it.
      Although it is a cool UDF example, it is better to write a formula to to sum or count based the criteria behind the reason to colour, rather than counting the colours.
      VBA is an awesome skill to learn and I have an online course.
      Alan

      Reply
  12. Adam says

    29 January 2021 at 5:15 pm

    Hi
    I understand this is an old post but would you be able to help.
    I like the sum of color cells to be picked up when I use conditional formatting. It picks up the number of colored cells when I fill manually but not with conditional formatting.
    Thanks

    Reply
    • Alan Murray says

      29 January 2021 at 6:21 pm

      Hi Adam, yes unfortunately this does not work with Conditional Formatting.

      Reply
  13. VG says

    2 September 2021 at 7:40 am

    It works, but if I change the colors of the cells, formula/function doesn’t calculate it automatically until I hit enter on the function cell again. why?
    Under formulas menu, Calculation Option is set to Automatic though.

    Please suggest?
    Thank You

    Reply
    • Alan Murray says

      18 September 2021 at 7:53 pm

      UDF’s are only calculated when one of their input values is changed. Unfortunately, a change in colour is not a change in value. This link provides some workarounds but none are perfect.

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Recent Posts

  • Excel SWITCH Function – Nested IF Function Alternative
  • Sum Formulas Only in Excel
  • How to Calculate Age in Excel
  • Custom Sort in Excel
  • Case Sensitive Lookup with XLOOKUP in Excel
Advanced Excel Success book

Footer

Contact

[email protected]

LinkedIn Twitter YouTube

Useful Links

  • Home
  • Excel Online Courses
  • Tutorials
  • Sitemap
  • Privacy Policy
  • Jobs by Jooble

Course Topics

  • Blog
  • Charts
  • Courses
  • Dashboards
  • Downloads
  • Excel Tips
  • Formulas
  • Mail Merge
  • Office
  • PivotTables
  • Power BI
  • Power Pivot
  • Power Query
  • VBA

Join Our Mailing List

© Copyright 2009 Computergaga · All Rights Reserved ·