VBA & Macro Problems

Return Decimal and Currency Values from an Excel VBA Function

Steve KSteve K Nov 27, 2026 870 views

Question details

Preserve decimal amounts returned by a VBA function that sums cells by fill color and display the result as currency.

Return Decimal and Currency Values from an Excel VBA Function
Product
Microsoft Excel
Device & OS
not provided
Scenario
A worksheet uses a VBA function to total values in cells with a matching fill color.
Observed behavior
The function returns a Long value, so decimal amounts are lost or appear as whole numbers.
Before you start

Identify the VBA function and the cells it sums, then check whether the result cell is formatted as a whole number. Make sure macros are enabled for the workbook so the function can run.

Solution 1Recommended

Use Double for the VBA function and its accumulator

Use this approach to retain fractional values while summing matching fill colors, then display the result with two decimal places.

A Long stores whole numbers. Declaring both the function return type and the running total as Double allows the calculation to retain decimal fractions. Cell formatting controls the displayed value, not the underlying calculation.

The example compares each cell's direct fill color, not a color applied by conditional formatting.

1
Open the VBA function

In Excel, open Developer > Visual Basic, then locate the module containing SumColor.

2
Change the return and total types

Declare both the function and its accumulator as Double. For example, replace the function with: Function SumColor(Color As Range, TargetRange As Range) As Double Dim cell As Range Dim total As Double For Each cell In TargetRange.Cells If cell.Interior.Color = Color.Cells(1, 1).Interior.Color Then If Not IsError(cell.Value2) Then If IsNumeric(cell.Value2) Then total = total + CDbl(cell.Value2) End If End If End If Next cell SumColor = total End Function

3
Enter the function

In the worksheet, enter a formula such as =SumColor(A1,B1:B10), where A1 has the fill color to match and B1:B10 contains the values to sum.

4
Format the result

Select the result cell, open Format Cells with Ctrl+1, and choose Currency or Number with 2 decimal places.

Use Double for the VBA function and its accumulator
Check unexpected rounding: A value of 33,098.013 normally displays as 33,098.01 with two decimal places. If it displays as 33,098.02, inspect the underlying source values and any rounding in the VBA code. Changing a fill color alone may not trigger recalculation.
Free Microsoft Office alternative

Work with currency-formatted spreadsheets in WPS Office

WPS Office is a free, lightweight, easy-to-use Microsoft Office alternative with a familiar spreadsheet interface. Open compatible Excel files and format totals as currency while moving between office suites.

Format spreadsheet totals as currency or numbers with decimal placesCompatible with Microsoft Excel formats such as XLSX and XLSFamiliar spreadsheet interface for an easier transitionFree and lightweight office suite
microsoft office alternative - wps office

Frequently Asked Questions

Why does my VBA sum-by-color function return a whole number?

A Long return type or accumulator stores whole numbers. In Developer > Visual Basic, declare both the function and its running total as Double to retain fractional values.

How do I show the result as currency with cents?

Select the result cell, open Format Cells with Ctrl+1, and choose Currency with 2 decimal places. This changes the display, not the underlying value.

Should 33,098.013 display as 33,098.01 or 33,098.02?

With ordinary two-decimal formatting, 33,098.013 displays as 33,098.01. If you see 33,098.02, check the underlying values and any explicit rounding in the VBA function.

Will the total update when I change a cell's fill color?

A fill-color change alone may not trigger Excel to recalculate the function. If the result does not update, force a full recalculation with Ctrl+Alt+F9.

Does the example count colors applied by conditional formatting?

No. The example checks each cell's direct Interior.Color property, so it does not necessarily match the color displayed by conditional formatting.