教书愚人 毁人不倦
教书愚人 毁人不倦
posts - 153,comments - 973,trackbacks - 21

最近去给一企业培训excel,有学员问到这样一个问题:要统计一个数据区中某些毫无规律单元格数值的和怎么办?

解决思路如下:

1、          首先把需要统计的单元格字体颜色设置成在整个区域中不曾使用的颜色;

2、          然后调用下面的函数进行统计;

3、          参数说明:Rng:需要统计的整个区域;Rng2:具有需要统计单元格一致的字体颜色的单元格;

4、          vba编制的小函数如下:

Function SumByColor(Rng As Range, Rng2 As Range) As Double

    Dim R As Range

    Dim Total As Double

    For Each R In Rng.Cells

        If R.Font.Color = Rng2.Font.Color Then

            Total = Total + R.Value

        End If

    Next R

    SumByColor = Total

End Function

Function CountByColor(Rng As Range,Rng2 As Range) As Long

    Dim R As Range

    Dim Count As Long

    For Each R In Rng.Cells

        If R.Font.Color = Rng2.Font.Color Then

            Count = Count + 1

        End If

    Next R

    CountByColor = Count

End Function

5、          打开excelVB代码编写环境;

6、          进入VB编程环境,插入模块;

7、          把上面的程序copy到下面位置:

 

8、          按照以下实例验证看看效果吧!

 

posted on 2006-09-20 20:47 教书愚人 毁人不倦 阅读(222) 评论(7) 编辑 收藏