在excel的使用过程中,会用到一些自定义函数,可以使用宏轻松的实现这些功能,问题是必须使用“启用宏的excel”,这样用户每次打开时都要启用宏。

现用以按背景色计划为例,解决以上问题:

1.新建一个空白的excel,按alt+F11,打开VB界面,点击“插入”---“模块”---录入以下代码:

//这个方法是计算相同颜色的单元格个数

Function CountColor(col As Range, countrange As Range) As Integer
Dim icell As Range
Application.Volatile
For Each icell In countrange
If icell.Interior.ColorIndex = col.Interior.ColorIndex Then
CountColor = CountColor + 1
End If
Next icell
End Function

//这个方法是计算相同颜色的单元格中的数据之和
Function SumColor(col As Range, sumrange As Range) As Integer
Dim icell As Range
Application.Volatile
For Each icell In sumrange
If icell.Interior.ColorIndex = col.Interior.ColorIndex Then
SumColor = Application.Sum(icell) + SumColor
End If
Next icell
End Function

在VB中点击保存,保存excel时选择“另存为”--“Excel 加载宏(*.xlam)”,把这个excel存储为sum.xlam文件。

2.打开要计算的excel,在此中选择开发工具--加载宏--浏览--选择sum.xlam,选中,点确认。

3.完成。

4.这样操作以后,只要打开excel就可以使用sumColor这个函数,不用启用宏。

posted on 2014-03-10 15:30  小傻瓜  阅读(2544)  评论(0编辑  收藏  举报