VBA & Macro Problems

Fix Excel VBA Sum-by-Color Macro Rounding to Whole Numbers

Tauseeq MagsiTauseeq Magsi Oct 26, 2026 869 views

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.

How to Fix an Excel VBA Sum-by-Color Macro That Rounds 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.
Before you start

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.

Solution 1Recommended

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.

1
Open the VBA Editor

Open your Excel workbook and press 'ALT + F11' on your keyboard to launch the Microsoft Visual Basic for Applications (VBA) editor.

2
Locate the Macro Code

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.

3
Find the Variable Declarations

Look for lines starting with 'Dim' that define your summing variables, such as 'Dim Total As Integer' or 'Dim Total As Long'.

4
Update to Double

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').

5
Save and Recalculate

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.

Change VBA Variable Data Types to Double
Data Types Fixed: Using the 'Double' data type guarantees that your macro retains high-precision decimal values, eliminating unwanted rounding during calculations.
Advanced Macro Support

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. 1. Open your workbook: Launch WPS Spreadsheet and open the Excel file (.xlsm or .xls) containing your sum-by-color macro.
  2. 2. Access the Macro Editor: Navigate to the 'Tools' tab on the top ribbon and click on 'Macros' to launch the VBA Editor.
  3. 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. 4. Save and Refresh: Save the modified macro, close the editor, and refresh your worksheet to instantly view precise decimal calculations.
Highly compatible with Microsoft Excel file formats and VBA macrosBuilt-in developer tools for editing and debugging custom scriptsProcess large datasets and custom mathematical formulas efficientlyFamiliar user interface ensuring a zero-learning-curve transition
QA img-9

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.