• Skip to main content
  • Skip to header right navigation
  • Skip to site footer

My Online Training Hub

Learn Dashboards, Excel, Power BI, Power Query, Power Pivot

  • Courses
  • Pricing
    • Free Courses
    • Power BI Course
    • Excel Power Query Course
    • Power Pivot and DAX Course
    • Excel Dashboard Course
    • Excel PivotTable Course – Quick Start
    • Advanced Excel Formulas Course
    • Excel Expert Advanced Excel Training
    • Excel Tables Course
    • Excel, Word, Outlook
    • Financial Modelling Course
    • Excel PivotTable Course
    • Excel for Customer Service Professionals
    • Excel for Operations Management Course
    • Excel for Decision Making Under Uncertainty Course
    • Excel for Finance Course
    • Excel Analysis ToolPak Course
    • Multi-User Pricing
  • Resources
    • Free Downloads
    • Excel Functions Explained
    • Excel Formulas
    • Excel Add-ins
    • IF Function
      • Excel IF Statement Explained
      • Excel IF AND OR Functions
      • IF Formula Builder
    • Time & Dates in Excel
      • Excel Date & Time
      • Calculating Time in Excel
      • Excel Time Calculation Tricks
      • Excel Date and Time Formatting
    • Excel Keyboard Shortcuts
    • Excel Custom Number Format Guide
    • Pivot Tables Guide
    • VLOOKUP Guide
    • ALT Codes
    • Excel VBA & Macros
    • Excel User Forms
    • VBA String Functions
  • Members
    • Login
    • Password Reset
  • Blog
  • Excel Webinars
  • Excel Forum
    • Register as Forum Member

Function to Count Colored Cells|VBA & Macros|Excel Forum|My Online Training Hub

You are here: Home / Function to Count Colored Cells|VBA & Macros|Excel Forum|My Online Training Hub
Avatar
sp_LogInOut Log In sp_Registration Register
sp_Search Search
Advanced Search|Last Search Results
Search
Forum Scope




Match



Forum Options



Minimum search word length is 3 characters - maximum search word length is 84 characters
sp_Search Search
sp_RankInfo
Lost password?
sp_CrumbsHome HomeExcel ForumVBA & MacrosFunction to Count Colored Cells
sp_PrintTopic sp_TopicIcon
Function to Count Colored Cells
Avatar
Ali Shoaib
Member
Members
Level 0
Forum Posts: 8
Member Since:
February 20, 2020
sp_UserOfflineSmall Offline
1
July 3, 2020 - 4:50 am
sp_Permalink sp_Print

I am looking for a code that will paste automatically =COUNTIFCOLOUR formula to Range("C1:AZ1").

First add the row on top across all sheets then paste the formula to Range("C1:AZ1") where formula will count colored cells from C1:C5000, D1:D5000, E1:E5000 and so on.

Here are two RGB which formula will count RGB(244, 204, 204) & RGB(183, 225, 205)

 

 

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

Avatar
Philip Treacy
Admin
Level 10
Forum Posts: 1518
Member Since:
October 5, 2010
sp_UserOfflineSmall Offline
2
July 3, 2020 - 8:12 am
sp_Permalink sp_Print

Hi HSAR,

Please supply workbooks when you are referring to data and it will be useful for those helping you.  Otherwise we have to type everything in by hand.

I'm not clear on what you are asking.  You want to count colored cells?

You can use this VBA Count, Sum and Average Colored Cells

or this non-VBA approach Count Colored Cells with SUBTOTAL

Regards

Phil

Avatar
Ali Shoaib
Member
Members
Level 0
Forum Posts: 8
Member Since:
February 20, 2020
sp_UserOfflineSmall Offline
3
July 3, 2020 - 9:35 pm
sp_Permalink sp_Print

Can you please edit this code right now the code is counting the single column and gives  accurate result when i add two or more columns then it gives same value for each column.

 

 

Public Sub CountColorCells()
'Variable declaration
Dim rng As Range
Dim lColorCounter As Long
Dim rngCell As Range
'Set the range
Set rng = Sheet2.Range("J2:J1000, K2:K1000")

'loop throught each cell in the range
For Each rngCell In rng
'Checking Yellor color
If Cells(rngCell.Row, rngCell.Column).DisplayFormat.Interior.Color = RGB(183, 225, 205) Then
lColorCounter = lColorCounter + 1
End If
Next
'Display the value in cell
Sheet2.Range("J1,K1") = lColorCounter

End Sub

Avatar
Philip Treacy
Admin
Level 10
Forum Posts: 1518
Member Since:
October 5, 2010
sp_UserOfflineSmall Offline
4
July 3, 2020 - 11:26 pm
sp_Permalink sp_Print

Hi HSAR,

You haven't supplied a workbook.

That VBA code isn't valid so it can't be giving an accurate result.

Did you try the code I linked to?

You haven't answered my question.

Regards

Phil

Avatar
Ali Shoaib
Member
Members
Level 0
Forum Posts: 8
Member Since:
February 20, 2020
sp_UserOfflineSmall Offline
5
July 6, 2020 - 9:05 pm
sp_Permalink sp_Print sp_EditHistory

Dear, Philip Treacy

I have also been using same code to count colored based cell  as you provided but right now i am looking for above 1 that i want you to modify. here is workbook

Avatar
Philip Treacy
Admin
Level 10
Forum Posts: 1518
Member Since:
October 5, 2010
sp_UserOfflineSmall Offline
6
July 6, 2020 - 11:00 pm
sp_Permalink sp_Print

Hi Ali,

Please see the attached workbook.  It uses the COUNTIFCOLOUR function and takes 2 arguments.  The 1st is the cell that has the colour you want to count, the 2nd is range containing cells that may have the colour from the first argument.

On Row 1 of Sheet 2 there are 3 examples of how the function works e.g. in H1 

=COUNTIFCOLOUR(G1,$A$3:$AC$42)

counts the 8 dark red cells in the range $A$3:$AC$42.

Regards

Phil

sp_Feed
Go to top
Forum Timezone: Australia/Brisbane
Most Users Ever Online: 245
Currently Online: Richard West, Clayton Watson
Guest(s) 9
Currently Browsing this Page:
1 Guest(s)
Top Posters:
SunnyKow: 1432
Anders Sehlstedt: 871
Purfleet: 412
Frans Visser: 346
David_Ng: 306
lea cohen: 219
Jessica Stewart: 204
A.Maurizio: 202
Aye Mu: 201
jaryszek: 183
Newest Members:
Murat Hasanoglu
Brett Dryland
Saeed Aldousari
Bhuwan Devkota
Kathryn Patton
Maria Conatser
Jefferson Granemann
Glen Coulthard
Nikki Fox
Rachele Dickie
Forum Stats:
Groups: 3
Forums: 24
Topics: 6222
Posts: 27291

 

Member Stats:
Guest Posters: 49
Members: 31913
Moderators: 3
Admins: 4
Administrators: Mynda Treacy, Philip Treacy, Catalin Bombea, FT
Moderators: MOTH Support, Velouria, Riny van Eekelen
© Simple:Press —sp_Information

Sidebar

Blog Categories

  • Excel
  • Excel Charts
  • Excel Dashboard
  • Excel Formulas
  • Excel PivotTables
  • Excel Shortcuts
  • Excel VBA
  • General Tips
  • Online Training
  • Outlook
  • Power Apps
  • Power Automate
  • Power BI
  • Power Pivot
  • Power Query
microsoft mvp logo
trustpilot excellent rating
Secured by Sucuri Badge
MyOnlineTrainingHub on YouTube Mynda Treacy on Linked In Mynda Treacy on Instagram Mynda Treacy on Twitter Mynda Treacy on Pinterest MyOnlineTrainingHub on Facebook
 

Company

  • About My Online Training Hub
  • Disclosure Statement
  • Frequently Asked Questions
  • Guarantee
  • Privacy Policy
  • Terms & Conditions
  • Testimonials
  • Become an Affiliate

Support

  • Contact
  • Forum
  • Helpdesk - For Technical Issues

Copyright © 2023 · My Online Training Hub · All Rights Reserved. Microsoft and the Microsoft Office logo are trademarks or registered trademarks of Microsoft Corporation in the United States and/or other countries. Product names, logos, brands, and other trademarks featured or referred to within this website are the property of their respective trademark holders.