{"id":244493,"date":"2023-04-11T17:27:20","date_gmt":"2023-04-11T17:27:20","guid":{"rendered":"https:\/\/vmlogger.com\/excel\/?p=244493"},"modified":"2023-04-11T17:27:20","modified_gmt":"2023-04-11T17:27:20","slug":"custom-excel-formula-to-count-based-on-color","status":"publish","type":"post","link":"https:\/\/vmlogger.com\/excel\/2023\/04\/custom-excel-formula-to-count-based-on-color\/","title":{"rendered":"Custom Excel Formula to Count based on Color"},"content":{"rendered":"

Dear Readers,<\/p>\n

Last Friday, my wife, Shagun Sharma, who is a Vlogger<\/a><\/span> shared an excel sheet with some data and a problem to solve, which can save some time of hers. <\/p>\n

\n

Problem Statement:<\/h2>\n

She had created an Excel sheet for her planning where she used to highlight some of the cells with certain colors as a background color for her readability purposes. Note that, this color filling [background color] was done by her manually over a period of time and not by conditional formatting, etc. Now, that she had a bigger sheet with many rows and columns, she wanted to count only those cells where she had filled in specific colors as the background color.\n<\/p><\/div>\n

Excel formula to count cells with specific colors<\/h2>\n

In order to count all such cells with a specific background color, I defined a user-defined function.<\/a> to count the number of cells with a specific background color.<\/p>\n

\nThe background color of a cell is stored in cell.Interior.ColorIndex<\/code> in Excel VBA. This ColorIndex<\/code>, as the name suggests stores the index in a numerical value assigned for each color. Therefore, we start counting, we need to define a function that can return you the ColorIndex<\/code> of the background color of a cell.\n<\/p>\n

VBA to get the Color Index of background color of a cell<\/h4>\n

Following is the simple, user-defined function, that will return the ColorIndex<\/code> of provided cell address<\/p>\n

\r\nPublic Function getColorIndex(ByVal cell As Range) As Integer\r\n    getColorIndex = cell.Interior.ColorIndex\r\nEnd Function\r\n<\/pre>\n

After running the above custom formula, you can see the color index of green<\/code> color is returned i.e. 50<\/code>.
\n

\"Green

Cell Interior color index<\/p><\/div><\/p>\n

Using the above custom function, now we can define another customer formula, which can return the count.<\/p>\n

\r\nPublic Function getColorCount(ByVal cell As Range, ByVal hex As Long) As Integer\r\n    Count = 0\r\n    For Each cell In cell.Cells\r\n        If (cell.Interior.ColorIndex = hex) Then\r\n            Count = Count + 1\r\n        End If\r\n    Next\r\n    getColorCount = Count\r\nEnd Function\r\n<\/pre>\n

After running the above custom formula, you can get the count of green color cells.
\n

\"Get

Get Count of colored background<\/p><\/div><\/p>\n

\n

How to use this code as a formula?<\/h3>\n

Refer to the bellow article to know how to use the above code in order to create a custom formula. How to create a custom formula?<\/a><\/p>\n

What is User Defined Function (UDF) ?<\/h3>\n

Microsoft Excel allows us to create our own functions, based on our own needs. Such functions are called \u201cUser Defined Functions\u201d (UDFs) or Custom Functions.<\/p>\n

These functions behave exactly like Excel\u2019s inbuilt formulas. These are helpful in simplifying complicated calculations or string-related stuff. Sometimes, you might have felt, \u201cI wish, Excel had a built-in formula for this calculation\u201d !! No worry, you can create your own formula.. and through all the dirty code behind the bar<\/p>\n<\/div>\n<\/span>","protected":false},"excerpt":{"rendered":"

Dear Readers, Last Friday, my wife, Shagun Sharma, who is a Vlogger shared an excel sheet with some data and a problem to solve, which can save some time of hers. Problem Statement: She had created an Excel sheet for her planning where she used to highlight some of the cells with certain colors as […]<\/p>\n","protected":false},"author":45,"featured_media":244505,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","footnotes":""},"categories":[1672,1246,1676,1682,2056],"tags":[],"class_list":["post-244493","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-excel-formula","category-macro","category-excel-tips","category-popular-articles","category-udf"],"yoast_head":"\nCustom Excel Formula to Count based on Color - Let's excel in Excel<\/title>\n<meta name=\"description\" content=\"How to count cells based on background color of cells | Excel formula to count based on color | Formula to get color index of background color of a cell\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/vmlogger.com\/excel\/2023\/04\/custom-excel-formula-to-count-based-on-color\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Custom Excel Formula to Count based on Color\" \/>\n<meta property=\"og:description\" content=\"How to count cells based on background color of cells | Excel formula to count based on color | Formula to get color index of background color of a cell\" \/>\n<meta property=\"og:url\" content=\"https:\/\/vmlogger.com\/excel\/2023\/04\/custom-excel-formula-to-count-based-on-color\/\" \/>\n<meta property=\"og:site_name\" content=\"Let's excel in Excel\" \/>\n<meta property=\"article:publisher\" content=\"http:\/\/www.facebook.com\/vmlogger\" \/>\n<meta property=\"article:author\" content=\"http:\/\/www.facebook.com\/vmlogger\" \/>\n<meta property=\"article:published_time\" content=\"2023-04-11T17:27:20+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/vmlogger.com\/excel\/wp-content\/uploads\/sites\/11\/2023\/04\/count-same-color-cell.png\" \/>\n\t<meta property=\"og:image:width\" content=\"2560\" \/>\n\t<meta property=\"og:image:height\" content=\"1440\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Vishwamitra Mishra\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/www.twitter.com\/learnexcelmacro\" \/>\n<meta name=\"twitter:site\" content=\"@learnexcelmacro\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Vishwamitra Mishra\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/vmlogger.com\/excel\/2023\/04\/custom-excel-formula-to-count-based-on-color\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/vmlogger.com\/excel\/2023\/04\/custom-excel-formula-to-count-based-on-color\/\"},\"author\":{\"name\":\"Vishwamitra Mishra\",\"@id\":\"https:\/\/vmlogger.com\/excel\/#\/schema\/person\/7500a107b0b2d35a8492acf0d11fc8e5\"},\"headline\":\"Custom Excel Formula to Count based on Color\",\"datePublished\":\"2023-04-11T17:27:20+00:00\",\"dateModified\":\"2023-04-11T17:27:20+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/vmlogger.com\/excel\/2023\/04\/custom-excel-formula-to-count-based-on-color\/\"},\"wordCount\":424,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/vmlogger.com\/excel\/#\/schema\/person\/7500a107b0b2d35a8492acf0d11fc8e5\"},\"image\":{\"@id\":\"https:\/\/vmlogger.com\/excel\/2023\/04\/custom-excel-formula-to-count-based-on-color\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/vmlogger.com\/excel\/wp-content\/uploads\/sites\/11\/2023\/04\/count-same-color-cell.png\",\"articleSection\":[\"Excel Formula\",\"Excel Macro\",\"Excel Tips\",\"Popular Articles\",\"User Defined Function\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/vmlogger.com\/excel\/2023\/04\/custom-excel-formula-to-count-based-on-color\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/vmlogger.com\/excel\/2023\/04\/custom-excel-formula-to-count-based-on-color\/\",\"url\":\"https:\/\/vmlogger.com\/excel\/2023\/04\/custom-excel-formula-to-count-based-on-color\/\",\"name\":\"Custom Excel Formula to Count based on Color - Let's excel in Excel\",\"isPartOf\":{\"@id\":\"https:\/\/vmlogger.com\/excel\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/vmlogger.com\/excel\/2023\/04\/custom-excel-formula-to-count-based-on-color\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/vmlogger.com\/excel\/2023\/04\/custom-excel-formula-to-count-based-on-color\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/vmlogger.com\/excel\/wp-content\/uploads\/sites\/11\/2023\/04\/count-same-color-cell.png\",\"datePublished\":\"2023-04-11T17:27:20+00:00\",\"dateModified\":\"2023-04-11T17:27:20+00:00\",\"description\":\"How to count cells based on background color of cells | Excel formula to count based on color | Formula to get color index of background color of a cell\",\"breadcrumb\":{\"@id\":\"https:\/\/vmlogger.com\/excel\/2023\/04\/custom-excel-formula-to-count-based-on-color\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/vmlogger.com\/excel\/2023\/04\/custom-excel-formula-to-count-based-on-color\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/vmlogger.com\/excel\/2023\/04\/custom-excel-formula-to-count-based-on-color\/#primaryimage\",\"url\":\"https:\/\/vmlogger.com\/excel\/wp-content\/uploads\/sites\/11\/2023\/04\/count-same-color-cell.png\",\"contentUrl\":\"https:\/\/vmlogger.com\/excel\/wp-content\/uploads\/sites\/11\/2023\/04\/count-same-color-cell.png\",\"width\":2560,\"height\":1440,\"caption\":\"Custom formula to count Cells\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/vmlogger.com\/excel\/2023\/04\/custom-excel-formula-to-count-based-on-color\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/vmlogger.com\/excel\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Excel Macro\",\"item\":\"https:\/\/vmlogger.com\/excel\/macro\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Custom Excel Formula to Count based on Color\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/vmlogger.com\/excel\/#website\",\"url\":\"https:\/\/vmlogger.com\/excel\/\",\"name\":\"Let's excel in Excel\",\"description\":\"Let's share knowledge\",\"publisher\":{\"@id\":\"https:\/\/vmlogger.com\/excel\/#\/schema\/person\/7500a107b0b2d35a8492acf0d11fc8e5\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/vmlogger.com\/excel\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/vmlogger.com\/excel\/#\/schema\/person\/7500a107b0b2d35a8492acf0d11fc8e5\",\"name\":\"Vishwamitra Mishra\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/vmlogger.com\/excel\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/vmlogger.com\/excel\/wp-content\/uploads\/sites\/11\/2022\/07\/avataaars-1.png\",\"contentUrl\":\"https:\/\/vmlogger.com\/excel\/wp-content\/uploads\/sites\/11\/2022\/07\/avataaars-1.png\",\"width\":528,\"height\":560,\"caption\":\"Vishwamitra Mishra\"},\"logo\":{\"@id\":\"https:\/\/vmlogger.com\/excel\/#\/schema\/person\/image\/\"},\"description\":\"My name is Vishwamitra Mishra. Friends Call me Vishwa. I hold a Bachelor\u2019s Degree in Computer Science from D.A.V.V. Indore & currently working as a Technical Lead having over 7 years of experience.\",\"sameAs\":[\"http:\/\/www.learnexcelmacro.com\",\"http:\/\/www.facebook.com\/vmlogger\",\"https:\/\/x.com\/https:\/\/www.twitter.com\/learnexcelmacro\",\"https:\/\/www.youtube.com\/c\/VMLogger\"],\"url\":\"https:\/\/vmlogger.com\/excel\/author\/vishwamitra\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Custom Excel Formula to Count based on Color - Let's excel in Excel","description":"How to count cells based on background color of cells | Excel formula to count based on color | Formula to get color index of background color of a cell","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/vmlogger.com\/excel\/2023\/04\/custom-excel-formula-to-count-based-on-color\/","og_locale":"en_US","og_type":"article","og_title":"Custom Excel Formula to Count based on Color","og_description":"How to count cells based on background color of cells | Excel formula to count based on color | Formula to get color index of background color of a cell","og_url":"https:\/\/vmlogger.com\/excel\/2023\/04\/custom-excel-formula-to-count-based-on-color\/","og_site_name":"Let's excel in Excel","article_publisher":"http:\/\/www.facebook.com\/vmlogger","article_author":"http:\/\/www.facebook.com\/vmlogger","article_published_time":"2023-04-11T17:27:20+00:00","og_image":[{"width":2560,"height":1440,"url":"https:\/\/vmlogger.com\/excel\/wp-content\/uploads\/sites\/11\/2023\/04\/count-same-color-cell.png","type":"image\/png"}],"author":"Vishwamitra Mishra","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/www.twitter.com\/learnexcelmacro","twitter_site":"@learnexcelmacro","twitter_misc":{"Written by":"Vishwamitra Mishra","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/vmlogger.com\/excel\/2023\/04\/custom-excel-formula-to-count-based-on-color\/#article","isPartOf":{"@id":"https:\/\/vmlogger.com\/excel\/2023\/04\/custom-excel-formula-to-count-based-on-color\/"},"author":{"name":"Vishwamitra Mishra","@id":"https:\/\/vmlogger.com\/excel\/#\/schema\/person\/7500a107b0b2d35a8492acf0d11fc8e5"},"headline":"Custom Excel Formula to Count based on Color","datePublished":"2023-04-11T17:27:20+00:00","dateModified":"2023-04-11T17:27:20+00:00","mainEntityOfPage":{"@id":"https:\/\/vmlogger.com\/excel\/2023\/04\/custom-excel-formula-to-count-based-on-color\/"},"wordCount":424,"commentCount":0,"publisher":{"@id":"https:\/\/vmlogger.com\/excel\/#\/schema\/person\/7500a107b0b2d35a8492acf0d11fc8e5"},"image":{"@id":"https:\/\/vmlogger.com\/excel\/2023\/04\/custom-excel-formula-to-count-based-on-color\/#primaryimage"},"thumbnailUrl":"https:\/\/vmlogger.com\/excel\/wp-content\/uploads\/sites\/11\/2023\/04\/count-same-color-cell.png","articleSection":["Excel Formula","Excel Macro","Excel Tips","Popular Articles","User Defined Function"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/vmlogger.com\/excel\/2023\/04\/custom-excel-formula-to-count-based-on-color\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/vmlogger.com\/excel\/2023\/04\/custom-excel-formula-to-count-based-on-color\/","url":"https:\/\/vmlogger.com\/excel\/2023\/04\/custom-excel-formula-to-count-based-on-color\/","name":"Custom Excel Formula to Count based on Color - Let's excel in Excel","isPartOf":{"@id":"https:\/\/vmlogger.com\/excel\/#website"},"primaryImageOfPage":{"@id":"https:\/\/vmlogger.com\/excel\/2023\/04\/custom-excel-formula-to-count-based-on-color\/#primaryimage"},"image":{"@id":"https:\/\/vmlogger.com\/excel\/2023\/04\/custom-excel-formula-to-count-based-on-color\/#primaryimage"},"thumbnailUrl":"https:\/\/vmlogger.com\/excel\/wp-content\/uploads\/sites\/11\/2023\/04\/count-same-color-cell.png","datePublished":"2023-04-11T17:27:20+00:00","dateModified":"2023-04-11T17:27:20+00:00","description":"How to count cells based on background color of cells | Excel formula to count based on color | Formula to get color index of background color of a cell","breadcrumb":{"@id":"https:\/\/vmlogger.com\/excel\/2023\/04\/custom-excel-formula-to-count-based-on-color\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/vmlogger.com\/excel\/2023\/04\/custom-excel-formula-to-count-based-on-color\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/vmlogger.com\/excel\/2023\/04\/custom-excel-formula-to-count-based-on-color\/#primaryimage","url":"https:\/\/vmlogger.com\/excel\/wp-content\/uploads\/sites\/11\/2023\/04\/count-same-color-cell.png","contentUrl":"https:\/\/vmlogger.com\/excel\/wp-content\/uploads\/sites\/11\/2023\/04\/count-same-color-cell.png","width":2560,"height":1440,"caption":"Custom formula to count Cells"},{"@type":"BreadcrumbList","@id":"https:\/\/vmlogger.com\/excel\/2023\/04\/custom-excel-formula-to-count-based-on-color\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/vmlogger.com\/excel\/"},{"@type":"ListItem","position":2,"name":"Excel Macro","item":"https:\/\/vmlogger.com\/excel\/macro\/"},{"@type":"ListItem","position":3,"name":"Custom Excel Formula to Count based on Color"}]},{"@type":"WebSite","@id":"https:\/\/vmlogger.com\/excel\/#website","url":"https:\/\/vmlogger.com\/excel\/","name":"Let's excel in Excel","description":"Let's share knowledge","publisher":{"@id":"https:\/\/vmlogger.com\/excel\/#\/schema\/person\/7500a107b0b2d35a8492acf0d11fc8e5"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/vmlogger.com\/excel\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/vmlogger.com\/excel\/#\/schema\/person\/7500a107b0b2d35a8492acf0d11fc8e5","name":"Vishwamitra Mishra","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/vmlogger.com\/excel\/#\/schema\/person\/image\/","url":"https:\/\/vmlogger.com\/excel\/wp-content\/uploads\/sites\/11\/2022\/07\/avataaars-1.png","contentUrl":"https:\/\/vmlogger.com\/excel\/wp-content\/uploads\/sites\/11\/2022\/07\/avataaars-1.png","width":528,"height":560,"caption":"Vishwamitra Mishra"},"logo":{"@id":"https:\/\/vmlogger.com\/excel\/#\/schema\/person\/image\/"},"description":"My name is Vishwamitra Mishra. Friends Call me Vishwa. I hold a Bachelor\u2019s Degree in Computer Science from D.A.V.V. Indore & currently working as a Technical Lead having over 7 years of experience.","sameAs":["http:\/\/www.learnexcelmacro.com","http:\/\/www.facebook.com\/vmlogger","https:\/\/x.com\/https:\/\/www.twitter.com\/learnexcelmacro","https:\/\/www.youtube.com\/c\/VMLogger"],"url":"https:\/\/vmlogger.com\/excel\/author\/vishwamitra\/"}]}},"_links":{"self":[{"href":"https:\/\/vmlogger.com\/excel\/wp-json\/wp\/v2\/posts\/244493"}],"collection":[{"href":"https:\/\/vmlogger.com\/excel\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/vmlogger.com\/excel\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/vmlogger.com\/excel\/wp-json\/wp\/v2\/users\/45"}],"replies":[{"embeddable":true,"href":"https:\/\/vmlogger.com\/excel\/wp-json\/wp\/v2\/comments?post=244493"}],"version-history":[{"count":0,"href":"https:\/\/vmlogger.com\/excel\/wp-json\/wp\/v2\/posts\/244493\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/vmlogger.com\/excel\/wp-json\/wp\/v2\/media\/244505"}],"wp:attachment":[{"href":"https:\/\/vmlogger.com\/excel\/wp-json\/wp\/v2\/media?parent=244493"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vmlogger.com\/excel\/wp-json\/wp\/v2\/categories?post=244493"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vmlogger.com\/excel\/wp-json\/wp\/v2\/tags?post=244493"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}