'參數(shù)不定的自定義函數(shù)
Function cheng(ParamArray n())
Dim num, k
k = 0
For Each num In n
k = k + num
Next num
cheng = k
End Function
'參數(shù)值默認(rèn)和參數(shù)缺省
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
'返回一個(gè)固定區(qū)間固定個(gè)數(shù)的不重復(fù)隨數(shù)
Function shuiji(maxnum, geshu) 'maxnum是區(qū)間最大的數(shù),geshu是返回多少個(gè)不重復(fù)的數(shù)
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
聯(lián)系客服