Private Sub Command1_Click()
Dim i As Double
Dim n As Double
Dim sum As Double
sum = 0
If Not IsNumeric(Text1.Text) Then '判断是否为数字,不是数字就弹出对话框并对出过程
Call msg '调用已经写好的过程
Exit Sub
End If
n = Text1.Text
If n < 1 Then '判断是否小于1 ,如果小于1就调用已经定义好的过程弹出对话框
Call msg
Exit Sub
End If
For n = 1 To n '求1!到n!阶乘循环n次
sum = sum + func(n) '调用求第n数的的阶乘的函数,计算前1!+n!的和
Next n
MsgBox "1!+2!+3!+......+n!为" & sum, vbInformation, "结果"
End Sub
'求第n个数的阶乘函数
Function func(n As Double) As Double
Dim i As Double
Dim jc As Double
jc = 1
For i = 1 To n
jc = jc * i
Next i
func = jc
End Function
'弹出对话框
Sub msg()
MsgBox "你输入数据不正确", vbInformation, "提示"
End Sub
把文本框中填写5就可以得到答案
Private Sub Command1_Click()
For i = 1 To 5 '5个阶乘
x = 1
For j = 1 To i '每个阶乘的值为x
x = x * j
Next j
s = s + x '把每个阶乘相加
Print s '显示结果
Next i
End Sub
我的空间里就有 为啥不先百度呢