Skip to main content
ExcelSum Values Based on Font ColorMath & TrigVBAUDFCustom Function

The Problem

Have you ever stared at a spreadsheet, meticulously color-coded by hand, and then realized you needed to Sum Values Based on Font Color? It's a common, frustrating scenario. Imagine your sales data, where urgent follow-up amounts are marked with red font, closed deals with green, and pending with blue. While your eyes can easily distinguish and mentally tally these, Excel's native SUM() function, powerful as it is, cannot directly "see" the font color of your cells.

What is Sum Values Based on Font Color? It's the essential capability to add up numeric values in a range, where the inclusion criterion is the specific font color applied to the cells. This is commonly used to aggregate data based on visual classifications that are not explicitly stored in cell values, such as status indicators or categories marked by formatting. You find yourself manually selecting cells, holding CTRL, and clicking away, or worse, re-entering data into a helper column just to use a standard SUMIF function. This manual process is not only tedious and error-prone but also completely counterproductive to the efficiency Excel promises.

Business Context & Real-World Use Case

In our experience as Excel consultants, we've repeatedly seen professionals across various industries grappling with this exact challenge. Consider a marketing team managing campaign budgets. Different expenses might be highlighted with unique font colors: red for over-budget items, green for under-budget, and blue for allocated but unspent funds. The marketing manager needs to quickly Sum Values Based on Font Color to understand total overages or remaining funds at a glance without having to modify the underlying data or apply complex filtering rules repeatedly.

Another typical scenario emerges in project management. Project tasks are often listed with their associated costs. If a task is at risk of exceeding its budget, its cost might be displayed in an orange font. A project manager needs to sum these "at-risk" costs to present to stakeholders and determine if additional resources are needed. Manually calculating this sum for a large project portfolio is a recipe for disaster, potentially leading to inaccurate forecasts, missed budget targets, and wasted time. We've witnessed teams spend hours on weekly reporting, meticulously filtering and summing, only to realize that a single font color change throws off their entire calculation. Automating this provides immense business value: it saves countless hours, minimizes human error, ensures real-time accuracy, and allows for dynamic reporting that immediately reflects visual changes, empowering faster and more informed decision-making.

The Ingredients: Understanding Sum Values Based on Font Color's Setup

To teach Excel to "see" and Sum Values Based on Font Color, we need to extend its capabilities beyond standard formulas. Excel's built-in SUM() function operates on values or arrays of values, not cell formatting. To bridge this gap, we'll employ a User-Defined Function (UDF) written in VBA (Visual Basic for Applications). This UDF acts as our "secret ingredient," a custom tool that can inspect cell properties like font color and then hand off the relevant values to our SUM() function.

Here's the exact syntax for the formula you'll use in your spreadsheet, once the custom function is in place:

=SUM(GetValuesByFontColorArray(targetRange, colorReferenceCell))

Let's break down the "parameters" (variables) our custom GetValuesByFontColorArray function needs:

Variables Description
targetRange This is the range of cells that you want to examine and potentially sum. It's the "basket" of numbers you're looking through to find those with a specific font color. (e.g., A1:A100)
colorReferenceCell This is a single cell whose font color serves as the criterion for summing. The function will look at this cell's font color and only include values from the targetRange that share this exact color. (e.g., B1, where B1 has the desired font color applied).

The GetValuesByFontColorArray function, which you'll create in VBA, will process the targetRange, identify numeric cells matching the colorReferenceCell's font color, and then return an array of these numbers. Finally, the standard SUM() function wraps this custom function, taking that array and effortlessly delivering your total.

The Secret Ingredient: VBA User-Defined Function (UDF)

This VBA code is what allows Excel to dynamically Sum Values Based on Font Color. You'll need to add this to your workbook:

' Place this code in a standard module (e.g., Module1) in the VBA editor (Alt+F11).
' Ensure your workbook is saved as a Macro-Enabled Workbook (.xlsm) for this function to persist.

Function GetValuesByFontColorArray(targetRange As Range, colorReferenceCell As Range) As Variant
    ' Declare the function as Volatile to ensure it recalculates whenever any formatting or data changes
    ' This is crucial because Excel doesn't automatically detect formatting changes for UDFs.
    Application.Volatile

    Dim cell As Range
    Dim resultCollection As New Collection
    Dim targetColor As Long
    Dim i As Long

    ' --- Input Validation ---
    ' Ensure colorReferenceCell is a single cell
    If colorReferenceCell Is Nothing Or colorReferenceCell.Cells.Count > 1 Then
        GetValuesByFontColorArray = CVErr(xlErrRef) ' Return #REF! for invalid color reference
        Exit Function
    End If

    ' Ensure targetRange is not empty
    If targetRange Is Nothing Or targetRange.Cells.Count = 0 Then
        GetValuesByFontColorArray = CVErr(xlErrValue) ' Return #VALUE! for empty target range
        Exit Function
    End If

    ' Get the font color from the reference cell.
    ' This will be our criterion for summing.
    targetColor = colorReferenceCell.Font.Color

    ' --- Process the Target Range ---
    ' Loop through each cell in the target range
    For Each cell In targetRange
        ' Check if the cell's font color matches the target color
        ' AND if the cell contains a numeric value
        If cell.Font.Color = targetColor And IsNumeric(cell.Value) Then
            resultCollection.Add cell.Value
        End If
    Next cell

    ' --- Convert Collection to Array for SUM function ---
    If resultCollection.Count > 0 Then
        Dim arr() As Variant
        ReDim arr(1 To resultCollection.Count) ' Resize array to fit collected values
        For i = 1 To resultCollection.Count
            arr(i) = resultCollection(i) ' Populate the array
        Next i
        GetValuesByFontColorArray = arr ' Return the array of matching numbers
    Else
        ' If no matching numbers are found, return an empty array.
        ' The SUM() function will then evaluate an empty array to 0, which is generally
        ' more user-friendly and avoids errors compared to returning #VALUE! in this case.
        GetValuesByFontColorArray = Array()
    End If
End Function

The Recipe: Step-by-Step Instructions

Let's walk through a concrete example. Suppose you have a list of monthly expenses, and you've manually colored the font of certain expenses to denote their status (e.g., critical expenses in red, reimbursed expenses in blue). You want to sum all the "critical" expenses.

Here's our sample data:

Expense Item Amount
Office Supplies 150
Software License 500
Client Lunch 75
Travel Expenses 300
Conference Fees 800
Consulting Service 1200

For this example, let's assume "Software License" (500) and "Consulting Service" (1200) have their amounts formatted with a red font.

  1. Open the VBA Editor: Press Alt + F11 on your keyboard. This will open the Microsoft Visual Basic for Applications window.

  2. Insert a New Module: In the VBA editor, in the left-hand Project Explorer pane, find your workbook's name (e.g., VBAProject (YourWorkbookName.xlsm)). Right-click on it, select Insert, then click Module. A new, blank module window will appear.

  3. Paste the VBA Code: Copy the GetValuesByFontColorArray function code provided in "The Ingredients" section above. Paste this entire code into the new module window.

  4. Close the VBA Editor: You can simply close the VBA editor window or go to File > Close and Return to Microsoft Excel.

  5. Save Your Workbook as Macro-Enabled: CRITICAL STEP! If you haven't already, save your Excel workbook as a "Macro-Enabled Workbook" (.xlsm). If you save it as a standard .xlsx file, your custom function will be lost. Go to File > Save As, choose a location, and select "Excel Macro-Enabled Workbook (*.xlsm)" from the "Save as type" dropdown.

  6. Set Up Your Spreadsheet Data:

    • Let's say your data is in cells B2:B7 (the "Amount" column).
    • In an empty cell, say C2, type the amount 500 and manually change its font color to red. This cell will serve as your colorReferenceCell.

    Your setup might look like this:

    A B C
    1 Expense Item Amount
    2 Office Supplies 150 500 (Red Font)
    3 Software License 500 (Red Font)
    4 Client Lunch 75
    5 Travel Expenses 300
    6 Conference Fees 800
    7 Consulting Service 1200 (Red Font)
    8
    9 Total Critical:
  7. Enter the Formula: In cell B9 (or any empty cell where you want the total), type the following formula:

    =SUM(GetValuesByFontColorArray(B2:B7, C2))

  8. Press Enter: The result 1700 will appear in cell B9.

Explanation:
The GetValuesByFontColorArray(B2:B7, C2) part of the formula first goes to cell C2 and reads its font color (red). Then, it scans the range B2:B7. It finds that cell B3 (500) and B7 (1200) have a red font and are numeric. It collects these values into an array {500, 1200}. Finally, the outer SUM() function takes this array and adds its elements together (500 + 1200 = 1700). This dynamically calculates the total of your critical expenses based on their font color.

Pro Tips: Level Up Your Skills

  • Understand Volatility: Our GetValuesByFontColorArray function uses Application.Volatile. This ensures it recalculates whenever any cell changes or the workbook recalculates. While helpful for immediate updates, use caution when scaling arrays over massive rows with volatile UDFs. They can significantly slow down your workbook if used excessively on very large ranges, as they recalculate frequently. For very large datasets, consider linking the function to a button that triggers a recalculation only when needed.
  • Color Consistency is Key: Excel differentiates between colors precisely. If you manually pick "Red" from the standard palette, but your reference cell uses a slightly different "custom red" (even if visually similar), the function won't find a match. Always use the exact same method to apply font colors if you intend to sum them. Using the Format Painter is a great way to ensure consistency.
  • Consider Conditional Formatting: If your font colors are applied based on certain criteria (e.g., numbers > 100 are red), it's often more efficient to Sum Values Based on Font Color using those underlying criteria with SUMIF or SUMIFS directly, rather than relying on a UDF that inspects formatting. For instance, =SUMIF(B2:B7, ">100", B2:B7) would sum all numbers greater than 100, which might coincide with your red font rule. This avoids VBA altogether.
  • Document Your UDFs: Always include comments in your VBA code to explain what the function does, its parameters, and any specific behaviors. This makes it much easier for you or others to maintain and troubleshoot the workbook later.

Troubleshooting: Common Errors & Fixes

When working with custom functions to Sum Values Based on Font Color, you might encounter a few hiccups. Here are the common errors and how to iron them out, focusing on the frequent #VALUE! error.

1. #VALUE! Error

  • Symptom: The formula in your cell displays #VALUE! or #REF!.

  • Why it happens (Cause 1: Macro-Enabled Workbook Not Saved): The most common reason for #VALUE! with UDFs is that you've entered the VBA code but haven't saved your workbook as a Macro-Enabled Workbook (.xlsm). When saved as .xlsx, Excel strips out all VBA code, meaning your custom function simply doesn't exist when the formula tries to call it.

    • How to fix it: Go to File > Save As, navigate to your desired location, and from the "Save as type" dropdown menu, select "Excel Macro-Enabled Workbook (*.xlsm)". Then, reopen the workbook if prompted.
  • Why it happens (Cause 2: Invalid Range or Non-Numeric Data): Your targetRange might contain non-numeric text values that the SUM function (or the UDF trying to collect numeric values) cannot process. While our UDF is designed to only Add IsNumeric(cell.Value), if the target range is entirely text or causes other internal type mismatches, it can lead to #VALUE!. Another cause is an empty targetRange being passed to the function.

    • How to fix it: Double-check that all cells within your targetRange that you expect to sum actually contain numbers. Ensure the range reference in your formula is correct and not empty (e.g., A1:A10 instead of A1:A1).
  • Why it happens (Cause 3: Invalid colorReferenceCell): If your colorReferenceCell is Nothing (an empty reference), or refers to a multi-cell range instead of a single cell, our UDF will return #REF! as a specific validation, but other issues with the reference might manifest as #VALUE!.

    • How to fix it: Ensure colorReferenceCell is a single, valid cell reference (e.g., C2, not C2:C3).

2. #NAME? Error

  • Symptom: The formula displays #NAME?.

  • Why it happens: This error means Excel cannot find a function with the name you've used in the formula. This usually points to a problem with your VBA setup or a typo.

    • How to fix it:
      1. Check for Typos: Carefully compare the name GetValuesByFontColorArray in your Excel formula to the function name in your VBA code. Even a slight misspelling will cause this error.
      2. Verify Module Placement: Ensure the VBA code is placed in a standard module (e.g., Module1, Module2), not a worksheet module (e.g., Sheet1 (Sheet1)) or a ThisWorkbook module. If it's in the wrong place, Excel won't recognize it as a global UDF.
      3. Macro Security Settings: In some corporate environments, macro security settings might be very strict, preventing any macros from running. Check your Excel Trust Center settings (File > Options > Trust Center > Trust Center Settings > Macro Settings). You might need to enable all macros (not recommended long-term) or "Disable all macros with notification" and then enable content when opening the file.

3. Incorrect Sum (Returns 0 or a Wrong Number)

  • Symptom: The formula calculates a number, but it's either 0 or not the sum you expected.

  • Why it happens (Cause 1: Font Color Mismatch): This is incredibly common. The font color of your colorReferenceCell might not exactly match the font color of the cells in your targetRange that you expect to be summed. Visually, two reds might look identical, but Excel's RGB values might differ.

    • How to fix it:
      1. Use Format Painter: The most reliable way to ensure exact color matches is to use the Format Painter. Select your colorReferenceCell, click the Format Painter tool on the Home tab, and then "paint" the format onto all the cells in your targetRange that should match that color.
      2. Verify Explicit Color: Ensure the cells actually have an explicit font color set, not just the default "automatic" color, which can vary in its underlying RGB value depending on the theme.
  • Why it happens (Cause 2: Range or Logic Error): Your targetRange might be incorrect, or the UDF itself might have a logical flaw (though our provided code is tested).

    • How to fix it: Double-check the targetRange in your formula to ensure it covers all the cells you intend to include. Step through the VBA code using the debugger (F8 in VBA editor) to watch the targetColor and how cell.Font.Color is being compared.

Quick Reference

Feature Description
Syntax =SUM(GetValuesByFontColorArray(targetRange, colorReferenceCell))
Core Idea Uses a custom VBA function to extract numeric values based on font color, then sums those values using the native SUM() function.
VBA Required Yes, a User-Defined Function (GetValuesByFontColorArray) must be added to a standard module in a macro-enabled workbook (.xlsm).
Key Use Case Dynamically sum values in a dataset that have been visually categorized by specific font colors for reporting or analysis.

Related Functions

👨‍💻

Written by The Head Chef

Former 10-year Financial Analyst who survived countless month-end closes. I build these recipes to save you from weekend-ruining spreadsheet errors.

Read the full story →

You might also find these useful 💡