Excel sums and counts according to color

1. Demand

There are cells marked with different colors in Excel. When counting, it needs to be counted according to the color.
insert image description here
It is definitely impossible to do it manually, and it seems that there is no idea with the help of Excel functions. In fact, this situation can be realized by using vba custom functions.

2. Implementation method

1. Code

Sum function code based on color

' 根据颜色求和
Function SumColor(i As Range, ary1 As Range)
Dim icell As Range
Application.Volatile
For Each icell In ary1
If icell.Interior.ColorIndex = i.Interior.ColorIndex Then
SumColor = Application.Sum(icell) + SumColor
End If
Next icell
End Function

Count function codes by color

' 根据颜色计数
Function CountColor(x As Range, ary2 As Range)
Application.Volatile
For Each i In ary2
If i.Interior.ColorIndex = x.Interior.ColorIndex Then
CountColor = CountColor + 1
End If
Next
End Function

Here we define two custom functions, SumColor and CountColor,

SumColor : Sums cells of a specified color

The first parameter: the color of the cell you want to sum

The second parameter: the data area to be summed

CountColor : Count the cells of the specified color

The first parameter: the color of the cell you want to count

The second parameter: the cell range you want to count

2. Create a custom function

Open 开发工具the tab –> click Visual Basic–> click 插入-模块–> copy the code to the window –> save
insert image description here
If you are also a Mac version of Excel and find that there is no 开发工具such tab, you can set it as follows.
Click Excel in the upper left corner, find 偏好设置-> 视图, and check it "开发工具"选项卡.
insert image description here

3. Using functions

There is no difference between using the function and using other Excel built-in functions, and it is similar to =SumColor(H3,A1:F9)this form, but it should be noted that there are two parameters here, the first parameter is a cell, and the second parameter is a range. The details are shown in the figure:
I will not take screenshots here, but use the ready-made animations on the Internet.
insert image description here

3. References

https://wenku.so.com/d/e776b4fd732883a47b03fe22f7c1a360
https://baijiahao.baidu.com/s?id=1654264602680369970&wfr=spider&for=pc

Guess you like

Origin blog.csdn.net/sdujava2011/article/details/131326297