How to Sum Excel Cells Without a Fill Color Using VBA
Question details
The user needs to calculate the sum of numerical values in a specific range of Excel cells, but only for cells that do not have a background fill color applied.

- Product
- Excel
- Device & OS
- not provided
- Scenario
- When managing spreadsheets where manual background color highlighting is used, users often need to aggregate totals for cells that are left unhighlighted (no fill).
- Observed behavior
- Standard built-in Excel worksheet functions do not support conditional summing based on cell background color or the Interior.ColorIndex property.
Ensure your workbook is saved as a Macro-Enabled Workbook (.xlsm) and that you have Developer tools or Macros enabled in your spreadsheet software settings.
Create a Custom VBA User-Defined Function (UDF)
Use a VBA script to create a custom formula that checks the interior color index of each cell before adding its value to the total sum.
Since standard formulas cannot read cell formatting, creating a custom User-Defined Function (UDF) via VBA is the most effective approach. This function iterates through a specified range and adds the values only if the cell's background is explicitly set to no fill.
Press the ALT + F11 keys on your keyboard simultaneously to open the Visual Basic for Applications (VBA) editor window.
In the VBA editor, click on 'Insert' in the top menu bar, and select 'Module' from the dropdown list to create a blank workspace for your code.
Copy and paste the following code into the new module window: Function SumNonFill(rng As Range) Dim rCel As Range For Each rCel In rng.Cells If rCel.Interior.ColorIndex = xlNone Then SumNonFill = SumNonFill + rCel.Value End If Next rCel End Function
Close the VBA editor and return to your worksheet. Select a blank cell and type the formula =SumNonFill(B2:B25) (adjusting the range B2:B25 to match your data), then press Enter.

Easily Manage VBA and Macros with WPS Office
WPS Office offers robust support for VBA and Macros, allowing you to run custom User-Defined Functions seamlessly. You can easily sum cells without a fill color and automate repetitive tasks using its integrated developer tools.
- 1. Download and Install: Download WPS Office from the official website and follow the simple installation instructions.
- 2. Open your Spreadsheet: Launch WPS Spreadsheet and open your existing .xlsx or .xlsm file.
- 3. Access Developer Tools: Navigate to the 'Developer' tab on the ribbon to access the VBA Editor and manage your macros.
- 4. Run Custom Functions: Use your custom VBA functions like =SumNonFill() directly in the worksheet to calculate your totals instantly.

Frequently Asked Questions
Why doesn't the function update automatically when I change a cell's color?
Changing a cell's fill color does not trigger the spreadsheet software to recalculate formulas. You will need to force a recalculation by pressing the F9 key or double-clicking the formula cell and pressing Enter.
Can I modify this code to sum cells with a specific color instead of no color?
Yes, you can alter the VBA script by replacing 'xlNone' with the specific ColorIndex number of the color you wish to sum (for example, use 3 for a standard red fill).
Does this VBA script work with conditional formatting colors?
No, the Interior.ColorIndex property only checks for manually applied background colors. It ignores colors that are applied through Conditional Formatting rules.
How do I save a workbook that contains this custom function?
You must save the file in a macro-enabled format, such as an Excel Macro-Enabled Workbook (.xlsm). If you save it as a standard .xlsx file, the VBA code will be removed upon closing.




