FREE Excel STUFF
SearchSearch Excel Content
Excel Help. Popular
NEW! Multiple Excel Search & Links
Excel Formulas
Excel Macros
Excel Newsletter
PRODUCTS
Up to $139.00 FREE!
Categories & SearchSearch for software
Excel Templates
Excel Add-ins
Excel Training
More....
OTHER
Excel Development


Sum/Count Cells By Fill Or Background Color in Excel


NEW! More Books..
Add to Google advanced search! Free Help!

Add Excel Answers & Search To Your Google Toolbar Details

Current Special! Complete Excel Excel Training Course for Excel 97 - Excel 2003, only $145.00. $59.95 Instant Buy/Download, 30 Day Money Back Guarantee & Free Excel Help for LIFE!

Back to: Excel Custom Function/Formulas . Got any Excel/VBA Questions? Free Excel Help

Want to Sum or Count cells that have a specified Fill Color? This can be done with the aid of a Custom Function.  A custom function, also called a User Defined Function, is a function that we write ourselves using VBA (Visual Basic for Applications).  Here is how

See Also: 2 Separate Functions Here and Excel Sort by Color

First open the Workbook in which you wish to count or sum cells by a fill color. Now go into the Visual Basic Editor via Tools>Macro>Visual Basic Editor (Alt+F11) and then, from within the Visual Basic Editor go  to Insert>Module to insert a standard module. Now, in this module, enter in the code as shown below;

Function ColorFunction(rColor As Range, rRange As Range, Optional SUM As Boolean)
	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

	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

You can now use the custom function (ColorFunction) like;

=ColorFunction($C$1,$A$1:$A$12,TRUE) to SUM the values in range of cells $A$1:$A$12 that have the same fill color as cell $C$1. The reason it will SUM in this example is because we have used TRUE as the last argument for the custom function.

To COUNT these cells that have the same fill color as cell $C$1 you could use:

=ColorFunction($C$1,$A$1:$A$12,FALSE) or =ColorFunction($C$1,$A$1:$A$12) by omitting the last argument our function will automatically default to using FALSE.

Be aware that the changing of a cells fill color will not cause the Custom Function to recalculate, even if you press F9 (Recalculates the whole Workbook). You will need to either, select the cell and re-enter the formula, or go to Edit>Replace and replace = with =, or use Ctrl+Alt+F9

Try also to avoid the use of Application.Volatile as it will not help in this case and only slow down Excel's calculation time.

See Also: 2 Separate Functions Here and Excel Sort by Color

See Also: Excel Duplication Manager Add-in | Excel Number Manager Add-in | Excel Text Manager Add-in | Excel Named Range Add-in Manager | Excel OzGrid Plus Add-in | Excel Time Sheet | Excel Time Wage and Pay book

Excel Dashboard Reports & Excel Dashboard Charts 50% Off

Special! Free Choice of Complete Excel Training Course OR Excel Add-ins Collection on all purchases totaling over $64.00. ALL purchases totaling over $150.00 gets you BOTH! Purchases MUST be made via this site. Send payment proof to special@ozgrid.com 31 days after purchase date.


Instant Download and Money Back Guarantee on Most Software

Excel Trader Package Technical Analysis in Excel With $139.00 of FREE software!

Add to Google Search Tips FREE Excel Help

Microsoft ® and Microsoft Excel ® are registered trademarks of Microsoft Corporation. OzGrid is in no way associated with Microsoft