vba不重復(fù)隨機(jī)數(shù)案例一:
下面的代碼,將在A1:A5000單元格產(chǎn)生1-5000的不重復(fù)隨機(jī)整數(shù)。
Private Sub vba不重復(fù)隨機(jī)數(shù)()
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不重復(fù)隨機(jī)數(shù)案例二:
在A1:A100中產(chǎn)生1-100的不重復(fù)隨機(jī)整數(shù),代碼如下:
Sub vba不重復(fù)隨機(jī)數(shù)()
Dim TempArr1(99) As Integer, TempArr2(0 To 99, 1 To 1) As Integer
Dim RndNumber As Integer, i As Integer
Randomize (Timer) '初始化隨機(jī)數(shù)生成器
For i = 0 To 99 '產(chǎn)生包含1-100的不重復(fù)的隨機(jī)數(shù)列
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中輸入這些數(shù)字
Range("a1:a100").Value = TempArr2
End Sub
vba不重復(fù)隨機(jī)數(shù)案例三:
在A1:A10單元格產(chǎn)生10個0-100的隨機(jī)整數(shù),以下是代碼:
Sub vba不重復(fù)隨機(jī)數(shù)()
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
核心關(guān)注:拓步ERP系統(tǒng)平臺是覆蓋了眾多的業(yè)務(wù)領(lǐng)域、行業(yè)應(yīng)用,蘊(yùn)涵了豐富的ERP管理思想,集成了ERP軟件業(yè)務(wù)管理理念,功能涉及供應(yīng)鏈、成本、制造、CRM、HR等眾多業(yè)務(wù)領(lǐng)域的管理,全面涵蓋了企業(yè)關(guān)注ERP管理系統(tǒng)的核心領(lǐng)域,是眾多中小企業(yè)信息化建設(shè)首選的ERP管理軟件信賴品牌。
轉(zhuǎn)載請注明出處:拓步ERP資訊網(wǎng)http://www.ezxoed.cn/
本文標(biāo)題:三個vba生成不重復(fù)隨機(jī)整數(shù)的案例
本文網(wǎng)址:http://www.ezxoed.cn/html/support/11139213040.html