Excel宏_实现对动态数据求均值和落在特定范围的数据的个数
2008-06-14 00:13
393 查看
5月16号晚,同学叫我帮忙写一个Excel宏,实现对动态数据的求均值和落在特定范围的数据的个数。由于我在大二学过VB(那时学VB主要是为了应付学校的计算机二级考试,过后就没动过了),但对VBA从没碰过,匆忙间也就在Microsoft 的在线帮助中看看,搞到早上5点左右,终于搞定,今天在此记下代码,以后如果有用,就方便了,如果他人觉得有用,请注明出处,谢谢!
Sub QIU_ZHI()
'要求:选中你要处理的数据,数据请不要放在前4列中
'功能:对数据求均值、0.9*均值、1.1*均值和落在这个范围内的数据的个数。
'注明:均值在3行3列,0.9*均值在4行3列,1.1*均值在5行3列,落在这个范围内的数据的个数在6行3列
'edit by Hsxzhe 080517
Dim mycell As Range '声明mycell是一个区域变量
'声明各变量,由于对VBA常用函数、属性不熟悉,故此宏中定义了大量变量来存储需要处理的数据
Dim data_sum As Double '总和
Dim pin_jun As Double '均值
Dim zero_p_nine_pinjun As Double '0.9*均值
Dim one_p_one_pinjun As Double '1.1*均值
Dim fanwei_feshu As Integer '落在这个范围内的数据的个数
Dim row_start As Integer '标记所选区域的开始行号
Dim row_end As Integer '标记所选区域的结束行号
Dim column_start As Integer '标记所选区域的开始列号
Dim column_end As Integer '标记所选区域的结束列号
Dim aero_num As Integer '标记所选区域的总个数
Dim row_temp As Integer
Dim column_temp As Integer
'初始化变量
data_sum = 0
pin_jun = 0
zero_p_nine_pinjun = 0
one_p_one_pinjun = 0
fanwei_geshu = 0
'求出总和
aero_num = 0
For Each mycell In Selection
If aero_num = 0 Then
row_start = mycell.Row
column_start = mycell.Column
End If
data_sum = data_sum + mycell.Value
aero_num = aero_num + 1
row_end = mycell.Row
column_end = mycell.Column
Next
'求出个数据
pin_jun = data_sum / aero_num
zero_p_nine_pinjun = 0.9 * pin_jun
one_p_one_pinjun = 1.1 * pin_jun
row_temp = 0
column_temp = 0
'求出落在这个范围内的数据的个数
For row_temp = row_start To row_end
For column_temp = column_start To column_end
If Cells(row_temp, column_temp).Value > zero_p_nine_pinjun And Cells(row_temp, column_temp).Value < one_p_one_pinjun Then
fanwei_geshu = fanwei_geshu + 1 '落在范围内,则统计个数
End If
Next
Next
Cells(3, 3) = pin_jun
Cells(4, 3) = zero_p_nine_pinjun
Cells(5, 3) = one_p_one_pinjun
Cells(6, 3) = fanwei_geshu
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''下面的是垃圾,只是格式处理''''''''''''''''''''''''''
Cells(3, 2) = "均值"
Cells(4, 2) = "0.9*均值"
Cells(5, 2) = "1.1*均值"
Cells(6, 2) = "范围个数"
Range(Cells(3, 2), Cells(6, 3)).Select
Range(Cells(3, 2), Cells(6, 3)).Font.Bold = True
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
End Sub
Sub QIU_ZHI()
'要求:选中你要处理的数据,数据请不要放在前4列中
'功能:对数据求均值、0.9*均值、1.1*均值和落在这个范围内的数据的个数。
'注明:均值在3行3列,0.9*均值在4行3列,1.1*均值在5行3列,落在这个范围内的数据的个数在6行3列
'edit by Hsxzhe 080517
Dim mycell As Range '声明mycell是一个区域变量
'声明各变量,由于对VBA常用函数、属性不熟悉,故此宏中定义了大量变量来存储需要处理的数据
Dim data_sum As Double '总和
Dim pin_jun As Double '均值
Dim zero_p_nine_pinjun As Double '0.9*均值
Dim one_p_one_pinjun As Double '1.1*均值
Dim fanwei_feshu As Integer '落在这个范围内的数据的个数
Dim row_start As Integer '标记所选区域的开始行号
Dim row_end As Integer '标记所选区域的结束行号
Dim column_start As Integer '标记所选区域的开始列号
Dim column_end As Integer '标记所选区域的结束列号
Dim aero_num As Integer '标记所选区域的总个数
Dim row_temp As Integer
Dim column_temp As Integer
'初始化变量
data_sum = 0
pin_jun = 0
zero_p_nine_pinjun = 0
one_p_one_pinjun = 0
fanwei_geshu = 0
'求出总和
aero_num = 0
For Each mycell In Selection
If aero_num = 0 Then
row_start = mycell.Row
column_start = mycell.Column
End If
data_sum = data_sum + mycell.Value
aero_num = aero_num + 1
row_end = mycell.Row
column_end = mycell.Column
Next
'求出个数据
pin_jun = data_sum / aero_num
zero_p_nine_pinjun = 0.9 * pin_jun
one_p_one_pinjun = 1.1 * pin_jun
row_temp = 0
column_temp = 0
'求出落在这个范围内的数据的个数
For row_temp = row_start To row_end
For column_temp = column_start To column_end
If Cells(row_temp, column_temp).Value > zero_p_nine_pinjun And Cells(row_temp, column_temp).Value < one_p_one_pinjun Then
fanwei_geshu = fanwei_geshu + 1 '落在范围内,则统计个数
End If
Next
Next
Cells(3, 3) = pin_jun
Cells(4, 3) = zero_p_nine_pinjun
Cells(5, 3) = one_p_one_pinjun
Cells(6, 3) = fanwei_geshu
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''下面的是垃圾,只是格式处理''''''''''''''''''''''''''
Cells(3, 2) = "均值"
Cells(4, 2) = "0.9*均值"
Cells(5, 2) = "1.1*均值"
Cells(6, 2) = "范围个数"
Range(Cells(3, 2), Cells(6, 3)).Select
Range(Cells(3, 2), Cells(6, 3)).Font.Bold = True
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
End Sub
相关文章推荐
- 大数据IMF传奇行动绝密课程第97课:使用SparkStreaming+SparkSQL实现在线动态计算出特定时间窗口
- 使用fusioncharts实现数据库的动态数据交互
- 用table,js实现web动态取数据并实现分页效果
- Extjs 实现动态添加gridPanel的列和数据
- Extjs4——实现动态的grid(即实现数据库表数据的显示)
- [Android]HelloCharts实现动态加载数据
- Android实现ListView数据动态加载的方法
- 数据结构与算法之动态数组实现堆栈
- C# JToken类的使用,实现解析动态json数据、遍历、查找
- 利用vue-router的动态路由和路由传值实现同一模板渲染不同数据
- ArcGIS Server 10.2 实战(一)Asp.net MVC与JSON数据妙用实现动态生成要素图层
- 【JavaEE企业应用实战学习记录】struts2实现登录并获取各个范围的数据
- 用公式实现动态设置图表的轴数据项
- Html5新标签datalist实现输入框跟后台数据库数据的动态匹配
- JS实现点击Radio动态更新table数据
- echart实现ajax动态数据加载(前台js代码)
- Highcharts、AJAX、JSON、JQuery实现动态数据交互显示图表柱形图
- android结合异步任务,动态加载图片,Json解析数据展示在ListView,并且实现按日期分类展示,借口回调
- java—将查询的结果封装成List<Map>与用回调函数实现数据的动态封装(44)
- R 实现GOstats 的方法(特定数据集合的分析)