VBA & Macro Problems

How to Sum Excel Cells Without a Fill Color Using VBA

Huma Ashraf ChHuma Ashraf Ch Nov 3, 2026 869 views

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.

How to Sum Excel Cells Without a Fill Color Using VBA
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.
Before you start

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.

Solution 1Recommended

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.

1
Open the VBA Editor

Press the ALT + F11 keys on your keyboard simultaneously to open the Visual Basic for Applications (VBA) editor window.

2
Insert a New Module

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.

3
Paste the VBA 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

4
Apply the Custom 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.

Create a Custom VBA User-Defined Function (UDF)
Conditional Formatting Limitation: This VBA function only detects manually applied fill colors. It will not work for cells whose colors are dynamically generated by Conditional Formatting rules.
Advanced Spreadsheet Tools

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. 1. Download and Install: Download WPS Office from the official website and follow the simple installation instructions.
  2. 2. Open your Spreadsheet: Launch WPS Spreadsheet and open your existing .xlsx or .xlsm file.
  3. 3. Access Developer Tools: Navigate to the 'Developer' tab on the ribbon to access the VBA Editor and manage your macros.
  4. 4. Run Custom Functions: Use your custom VBA functions like =SumNonFill() directly in the worksheet to calculate your totals instantly.
Fully compatible with Microsoft Excel VBA scripts and .xlsm formatsFree, lightweight, and easy-to-use spreadsheet editorFamiliar user interface makes transitioning from other software effortlessBuilt-in Developer tools to write, edit, and run macros smoothly
QA img-9

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.