Fix Excel VBA Sum-by-Color Macro Rounding to Whole Numbers
Question details
The user has a custom VBA macro in Excel designed to sum cells based on their background color, but it incorrectly returns whole numbers and discards decimal values.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- Calculating the total of specifically colored cells that contain currency or other decimal-based figures.
- Observed behavior
- The macro truncates or rounds the calculation, outputting only whole numbers and losing precise decimal data such as cents.
Ensure you have saved a backup copy of your Excel workbook before editing the VBA code, and confirm that your cells are formatted to display at least two decimal places.
Change VBA Variable Data Types to Double
Modify the variable declarations within your VBA script to allow floating-point decimal values instead of restricting the output to whole numbers.
In VBA, variables declared as 'Integer' or 'Long' are designed exclusively to store whole numbers. If your sum-by-color macro uses these data types for its intermediate totals or the final return value, any decimal values (like cents) will be automatically rounded or truncated. To fix this behavior, you must use the 'Double' data type, which supports precise floating-point numbers.
Open your Excel workbook and press 'ALT + F11' on your keyboard to launch the Microsoft Visual Basic for Applications (VBA) editor.
In the Project Explorer pane on the left, find the module that contains your sum-by-color function and double-click it to view the code.
Look for lines starting with 'Dim' that define your summing variables, such as 'Dim Total As Integer' or 'Dim Total As Long'.
Change 'Integer' or 'Long' to 'Double' (e.g., 'Dim Total As Double'). Ensure the function's return type is also set correctly (e.g., 'Function SumByColor(target As Range) As Double').
Click the Save icon in the VBA editor, return to your Excel worksheet, and press 'F9' to recalculate the formulas. The cells should now display the accurate decimal values.

Edit and Run VBA Macros Seamlessly in WPS Spreadsheet
WPS Office provides robust built-in macro editing tools, making it easy to open your existing workbooks, debug VBA scripts, and correct data types directly within a lightweight and responsive interface.
- 1. Open your workbook: Launch WPS Spreadsheet and open the Excel file (.xlsm or .xls) containing your sum-by-color macro.
- 2. Access the Macro Editor: Navigate to the 'Tools' tab on the top ribbon and click on 'Macros' to launch the VBA Editor.
- 3. Modify the Data Type: Locate your sum-by-color script in the code window and change the variable declarations from 'Integer' or 'Long' to 'Double'.
- 4. Save and Refresh: Save the modified macro, close the editor, and refresh your worksheet to instantly view precise decimal calculations.

Frequently Asked Questions
What is the difference between Integer, Long, and Double in VBA?
'Integer' and 'Long' are data types used exclusively for whole numbers, with 'Long' being able to accommodate much larger figures. 'Double' is a floating-point data type used for numbers that require fractions or decimals, making it ideal for financial or highly precise calculations.
Why does my Excel formula still ignore decimals after fixing the VBA code?
If your cells are formatted to show zero decimal places, Excel will display visually rounded whole numbers even if the underlying value contains decimals. Select the affected cells, right-click and choose 'Format Cells', go to the 'Number' tab, and increase the decimal places to 2.
Can I sum cells by color without using a VBA macro?
Excel does not have a native formula to sum by color. However, you can filter your data column by cell color using the standard Data Filter, and then use the SUBTOTAL function (e.g., =SUBTOTAL(109, A2:A100)) to sum only the currently visible colored rows.




