Recently some of our subscribers have requested us to share a VBA code that can sum the cells by its color. To help our subscribers and developers, we are sharing 2 codes that be used to sum the cells with specific color and returns the total sum of the matching color cells
Public Function SumByColor(rng As Range, Red As Long, Green As Long, Blue As Long) As Double
'Variable declaration
Dim dblSum As Double
Dim rngCell As Range
'Loop throught each cell in the range
For Each rngCell In rng
'Checking and sum color
If rngCell.Interior.Color = RGB(Red, Green, Blue) Then
If IsNumeric(rngCell.Value) = True Then
dblSum = dblSum + rngCell.Value
End If
End If
Next
'Return the value
SumByColor = dblSum
End Function
Public Function SumByColor(rng As Range, ColorCell As Range) As Double
'Variable declaration
Dim dblSum As Double
Dim rngCell As Range
'Loop throught each cell in the range
For Each rngCell In rng
'Checking and sum color
If rngCell.Interior.Color = ColorCell.Interior.Color Then
If IsNumeric(rngCell.Value) = True Then
dblSum = dblSum + rngCell.Value
End If
End If
Next
'Return the value
SumByColor = dblSum
End Function
If you want to use this code in your VBA tool, then follow below steps:
Step 1: Open the Excel file in which you want to copy this code
Step 2: Press Alt+F11 to open VBA editor
Step 3: Insert a new module from Insert > Module menu
Step 4: Paste the code in the module
Step 5: Now you are ready to use this code as Excel Function/Formula
Step 6: Type the formula in the cell where you want to get the sum specific colored cells
First Code Example: =SumByColor(A2:A11,146, 208, 80)
The first parameter of the formula is the range from which you want to sum the specific color cells. The second, third and fourth parameters are RGB code of the color.
Second Code Example: =SumByColor(A2:A11,A3)
The first parameter of the formula is the range from which you want to sum the specific color cells. The second parameter is the cell from which you want to compare the color.
Step 1: Select the cell which contain the color you want to use
Step 2: Right click and select ‘Format Cells…’
Step 3: In the Format Cells dialog box go to ‘Fill’ tab and click on ‘More Colors…’
Step 4: That’s all, in the ‘Color’ dialog box, you can view the RGB (Red, Green, Blue) codes of the color
Thanks for reading the article, subscribe us to get more VBA tricks
VBA Code To Delete All Shapes On A Excel sheet Here is a VBA code which deletes all the shapes from an Excel sheet. Code is simple but you have to be bit careful while…
How to use the compound interest formula in Excel and gives examples of how to calculate the future value of an investment with yearly, monthly, or daily interest. It also shows you step-by-step how to make your own Excel compound interest calculator.
Many tasks in Excel require comparing data in different cells. To do this, Excel offers six logical operators, also known as comparison operators. This tutorial will help you understand how these operators work and how to write efficient formulas for data analysis
VBA Code to check if folder exist Validation is one of the important parts of any programming language. As per few studies, 60% of the code is focused on validating input or output. In this…
Excel Dark mode reduces eye strain and makes the screen more comfortable to work on in high-contrast settings. Follow this easy tutorial to enable dark mode for both the Excel interface and the spreadsheet cells.
What is the Usage of sheet color in Excel? When we prepare a report or a dashboard it is easy to identify or analyze reports with a change of color sheet tabs. Analysts generally give…
Hi,
How to Count (D71) CONDITIONALLY FORMATTED RED COLOR CELL only (D40:D70)?
Pls guide…
Thanks
If you want to count from single cell then formula will be =SumByColor(D71,256,0,0)