vba不重复随机数案例一:
下面的代码,将在A1:A5000单元格产生1-5000的不重复随机整数。
Private Sub vba不重复随机数()
Dim Arr1(1 To 5000) As BooLEAN
Dim Arr2(1 To 5000, 0) As Integer
t = Timer
[A:A] = Empty
For i = 1 To 5000
num = Int(5000 * Rnd + 1)
Do While Arr1(num)
num = Int(5000 * Rnd + 1)
Loop
Arr1(num) = True
Arr2(i, 0) = num
Next i
[A1:A5000] = Arr2
End Sub
vba不重复随机数案例二:
在A1:A100中产生1-100的不重复随机整数,代码如下:
Sub vba不重复随机数()
Dim TempArr1(99) As Integer, TempArr2(0 To 99, 1 To 1) As Integer
Dim RndNumber As Integer, i As Integer
Randomize (Timer) '初始化随机数生成器
For i = 0 To 99 '产生包含1-100的不重复的随机数列
TempArr1(i) = i
Next i
For i = 99 To 0 Step -1
RndNumber = Int(i * Rnd)
TempArr2(99 - i, 1) = TempArr1(RndNumber) + 1
TempArr1(RndNumber) = TempArr1(i)
Next i
'在A1:A100中输入这些数字
Range("a1:a100").Value = TempArr2
End Sub
vba不重复随机数案例三:
在A1:A10单元格产生10个0-100的随机整数,以下是代码:
Sub vba不重复随机数()
Dim i As Long
i = 100
Dim num As Long
ReDim arr(i) As Long
ReDim Arr2(i, 0) As Long
Dim x As Long
For x = 0 To i
arr(x) = x
Next x
For x = 0 To i
num = Int(Rnd() * (i - x))
Arr2(x, 0) = arr(num)
arr(num) = arr(i - x)
Next x
Range("A1:A10") = Arr2
End Sub
核心关注:拓步ERP系统平台是覆盖了众多的业务领域、行业应用,蕴涵了丰富的ERP管理思想,集成了ERP软件业务管理理念,功能涉及供应链、成本、制造、CRM、HR等众多业务领域的管理,全面涵盖了企业关注ERP管理系统的核心领域,是众多中小企业信息化建设首选的ERP管理软件信赖品牌。
转载请注明出处:拓步ERP资讯网http://www.toberp.com/
本文标题:三个vba生成不重复随机整数的案例