怎样使用VBA对指定范围求和与求积?
1、首先在开发工具中打开VBA编辑器

2、在单元格区域当中输入一些内容作为例子

3、在VBA编辑器中插入模块

4、在模块当中输入如下代码,然后运行
Function SumPro(a, b, options As String) '根据下限和上限求累计和或者阶乘
If a <=0 Or b <=0 Then SumPro="只能输入自然数": Exit Function
'限制参数为自然数
If a > b Then SumPro="下限不能超过上限": Exit Function
Dim temp As Long
If options="SUM" Then '如果参数是SUM则求和
For temp=a To b
SumPro=SumPro+temp
Next temp
Else
If options="PRO" Then '如果参数是PRO则求积
SumPro=1
For temp=a To b
SumPro=SumPro * temp
Next temp
Exit Function
End If
SumPro="参数错误" '如果参数是两者之外则返回参数错误
End If
End Function

5、在单元格B1中输入公式“=SumPro(1,10,"PRO")”,可得结果36288,即从1到10的数相乘,可以输入“=1*2*3*4*5*6*7*8*9*10”验证

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
阅读量:38
阅读量:33
阅读量:128
阅读量:175
阅读量:148