1、首先我们要按ALT +F11键,进入VBE,然后插入模块,如下图。
2、在新建的模块中粘贴上代码:Function Color_sum(rng As Ra艘绒庳焰nge, rng2 As Range, n 帆歌达缒As Integer)'n = 1 表示进行加总,n=0 表示 计数Application.VolatileDim Ra As RangeDim myColor As DoubleDim mySum As DoubleDim myCount As DoublemyColor = rng2.Interior.ColorIndexFor Each Ra In rngIf Ra.Interior.ColorIndex = myColor ThenIf n = 1 ThenmySum = Ra + mySumEnd IfIf n = 0 ThenmySum = mySum + 1End IfEnd IfNextColor_sum = mySumEnd Function
3、在需要的单元格输入公式。加总=Color_sum($A$1:$C$26,$E4,1拘七呷憎)计数=Color_sum($A$1:$C$26,$E4,0)参数3 个,第一个是需要计算的区间,溟猫钽吟第二个是提取颜色的单元格,第三个选择加总还是计数。如图