Function jc(x) '这个就是自定义的阶乘函数,函数名为jc,参数为x,也就是阶乘的阶数
If x = 0 Then '最小阶数为0,0的阶乘是1
jc = 1
Exit Function '用于结束循环调用
Else
jc= x * jc(x - 1) '循环调用阶乘函数,直到调用的阶乘值为1
End If
End Function
也可以用VBA直接调用工作表函数WorkSheetFunction.FACT(100)
在A1单元格输入整数,在B1单元格输出结果:
Sub 阶乘()Dim s As Long, n As Integer, i As Integer
n = Val(Range("A1").Value)
s = 1
For i = 1 To n
s = s * i
Next i
Range("B1").Value = s
End Sub
第一个
sub 求和()dim arr, i as integer,he as integer
arr=[a3:b11]
for i=1 to ubound(arr)
if arr(i,1)="1001" or arr(i,1)="1003" then
he=he+arr(i,2)
end if
next
[b12]=he
end sub
第二个
Sub 阶乘()
Dim i As Integer, a As Integer, jc As Integer
a = [a1]
jc = 1
For i = 1 To a
jc = jc * i
Next
[a2] = jc
End Sub
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)