'参数不定的自定义函数
Function cheng(ParamArray n())
Dim num, k
k = 0
For Each num In n
k = k + num
Next num
cheng = k
End Function
'参数值默认和参数缺省
Function shuiji1(maxnum, geshu, Optional qo As Integer)
Dim d As New Dictionary
Dim num
Application.Volatile
Do
num = Int(Rnd() * maxnum + 1)
If qo = 0 Then
d(num) = ''
ElseIf qo = 2 Then
If num Mod 2 = 0 Then d(num) = ''
ElseIf qo = 1 Then
If Not num Mod 2 = 0 Then d(num) = ''
End If
Loop Until d.Count = geshu
shuiji1 = Application.Transpose(d.Keys)
End Function
Function shuiji2(maxnum, geshu, Optional qo As Integer = 2)
Dim d As New Dictionary
Dim num, m
Application.Volatile
m = 1
Do
num = Int(Rnd() * maxnum + 1)
If qo = 2 Then
If num Mod 2 = 0 Then d(num) = ''
ElseIf qo = 1 Then
If Not num Mod 2 = 0 Then d(num) = ''
Else
Exit Function
End If
Loop Until d.Count = geshu
shuiji2 = Application.Transpose(d.Keys)
End Function
'返回一个固定区间固定个数的不重复随数
Function shuiji(maxnum, geshu) 'maxnum是区间最大的数,geshu是返回多少个不重复的数
Dim d As New Dictionary
Dim num
Application.Volatile
Do
num = Int(Rnd() * maxnum + 1)
d(num) = ''
Loop Until d.Count = geshu
shuiji = Application.Transpose(d.Keys)
End Function
联系客服