Written by Allen Wyatt (last updated October 15, 2022)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021
Tiffany has a column that contains numeric values. Some of the cells have been filled with different colors. Some of those colors are explicit; some are applied through conditional formatting. She would like to be able to sum the cells that contain any fill color, regardless of the color. In other words, the sum should exclude only the cells that have no fill color.
One way to accomplish this task is by using the filtering capabilities of Excel. For instance, you could follow these steps:
=SUM(A2:A99)-SUBTOTAL(9,A2:A99)
The value returned from the formula is the sum of all the cells that use any fill color. This works because the SUM function returns of sum of all the cells in the range (in this case, A2:A99), and the SUBTOTAL function returns the sum of all the visible cells in the range. Subtract the visible (non-filled) cells from the total for the range, and you end up with the sum of all filled cells.
If you would prefer to not filter your data, then you could create a user-defined function to return the sum. The following would work great for this purpose:
Public Function SumFilled(Target As Range) Dim cell As Range Dim sAddr As String Application.Volatile For Each cell In Target sAddr = cell.Address(External:=True) If Evaluate("CellHasFill(""" & sAddr & """)") Then SumFilled = SumFilled + WorksheetFunction.Sum(cell) End If Next cell End Function
Private Function CellHasFill(Addr As String) As Boolean CellHasFill = (Range(Addr).DisplayFormat.Interior.ColorIndex <> xlColorIndexNone) End Function
Note that there is a UDF defined here (SumFilled) and a function that is called from the UDF. This arrangement is necessary because the heart of the macro is the .DisplayFormat.Interior.ColorIndex property which returns the color that is displayed in a cell, regardless of whether it is the result of an explicit fill or of a conditional format. This property—most likely because of a quirk (i.e., bug) in Excel—won't work directly in a UDF, though it will work in a hard-coded (non-UDF) macro. For more information on this and why this workaround approach was used, see the following thread at SubStack:
https://stackoverflow.com/questions/52265819/getting-cell-interior-color-fails-when-range-passed-from-worksheet-function/
Thus, to use the UDF you could put the following in your worksheet:
=SumFilled(AA2:A99)
If you want to use a non-UDF macro, then the following will work:
Public Sub ClrSum() Dim x As Double Set Rng = Range("A2:A99") x = 0 For Each c In Rng.Cells If c.DisplayFormat.Interior.ColorIndex <> xlColorIndexNone Then x = x + c.Value Next c MsgBox x End Sub
Run the macro, and it evaluates all the cells in the range A2:A99 and returns the sum displayed in a message box.
Finally, it should be noted that if you have the opportunity to rethink the organization of your worksheet, it would be advisable to not use color as data. In other words, you might consider adding a column to contain a numeric indicator as to the status of the values in your presently color-coded values. In this way you could easily use functions such as SUMIF to evaluate your data. If you want the color coding, then you could create conditional formatting that would modify color based on the value in the added status column.
Note:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (9859) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021.
Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!
It is not unusual to need to select two random items from a list. There are a couple of ways you can approach the task; ...
Discover MoreSometimes making sure that a reference in a formula doesn't get changed is not as simple as putting dollar signs in front ...
Discover MoreRemember your number line from your early years in school? Some numbers can be below zero (negative numbers) and others ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2022-10-17 09:52:03
J. Woolley
My Excel Toolbox includes array function HasFill(Target), where Target must be a range array like A1:C3 or a single cell like D4. HasFill returns a TRUE/FALSE array the same size as Target indicating whether each cell in Target has fill (explicit or conditional). Utilizing HasFill, here is a formula to sum cells in A2:A99 that contain fill:
=SUMPRODUCT(--HasFill(A2:A99),A2:A99)
The double unary operator converts from TRUE/FALSE to 1/0.
Here is an abbreviated version of HasFill:
Public Function HasFill(Target As Range) As Boolean()
Dim A() As Boolean, sAddr As String
Dim nRows As Long, nCols As Long, nR As Long, nC As Long
Application.Volatile
With Target.Areas(1)
nRows = .Rows.Count
nCols = .Columns.Count
ReDim A(1 To nRows, 1 To nCols)
For nR = 1 To nRows
For nC = 1 To nCols
sAddr = .Cells(nR, nC).Address(External:=True)
A(nR, nC) = Evaluate("CellHasFill(""" & sAddr & """)")
Next nC
Next nR
End With
HasFill = A
End Function
Notice HasFill utilizes Private Function CellHasFill, which is included in the Tip. If Target is not contiguous like (A1:C3,D4), only the first contiguous area applies.
See https://stackoverflow.com/a/52265973/10172433
and https://sites.google.com/view/MyExcelToolbox/
2022-10-15 10:55:49
J. Woolley
This Tip has some issues:
1. '=SumFilled(AA2:A99)' should be '=SumFilled(A2:A99)'
2. In Sub ClrSum:
2.a. Might add 'Dim Rng as Range, c as Range'
2.b. Change '...Then x = x + c.Value' to
'...Then x = x + WorksheetFunction.Sum(c)' or
'...Then x = WorksheetFunction.Sum(x,c)'
for the same reason as Function SumFilled; otherwise, a non-numeric or text cell value might cause trouble.
Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2024 Sharon Parq Associates, Inc.
Comments