EXCEL VBA生成不重复随机数,且越来越大
发布网友
发布时间:2022-04-23 13:09
我来回答
共3个回答
热心网友
时间:2023-10-14 22:58
Sub 随机数()
Dim rng As Range
For Each rng In Range("A1:A100")
rng = Application.Round(Application.RandBetween(100000 + 899.99 * (rng.Row - 1), 100000 + 899.99 * rng.Row), 0)
Next
End Sub
热心网友
时间:2023-10-14 22:59
那就不是随机了。假如第一个随机数就是999998,如何越来越大呢?
热心网友
时间:2023-10-14 22:59
随机数不重复可借助于字典;
随机数越来越大可借助于 Excel 的排序。
Option Explicit
Sub RandomNumbers()
Dim lngNum As Long
Dim objDic As Object
Const MIN_NUM As Long = 100000
Const MAX_NUM As Long = 999999
Randomize
Set objDic = CreateObject("Scripting.Dictionary")
Do Until objDic.Count = 100
lngNum = Int((MAX_NUM - MIN_NUM + 1) * Rnd + MIN_NUM)
If Not objDic.Exists(lngNum) Then
objDic.Add lngNum, Empty
End If
Loop
Application.ScreenUpdating = False
With Range("A1:A100")
.Value = WorksheetFunction.Transpose(objDic.Keys())
.Sort .Cells(1)
End With
Application.ScreenUpdating = True
Set objDic = Nothing
End Sub
热心网友
时间:2023-10-14 22:58
Sub 随机数()
Dim rng As Range
For Each rng In Range("A1:A100")
rng = Application.Round(Application.RandBetween(100000 + 899.99 * (rng.Row - 1), 100000 + 899.99 * rng.Row), 0)
Next
End Sub
热心网友
时间:2023-10-14 22:59
那就不是随机了。假如第一个随机数就是999998,如何越来越大呢?
热心网友
时间:2023-10-14 22:59
随机数不重复可借助于字典;
随机数越来越大可借助于 Excel 的排序。
Option Explicit
Sub RandomNumbers()
Dim lngNum As Long
Dim objDic As Object
Const MIN_NUM As Long = 100000
Const MAX_NUM As Long = 999999
Randomize
Set objDic = CreateObject("Scripting.Dictionary")
Do Until objDic.Count = 100
lngNum = Int((MAX_NUM - MIN_NUM + 1) * Rnd + MIN_NUM)
If Not objDic.Exists(lngNum) Then
objDic.Add lngNum, Empty
End If
Loop
Application.ScreenUpdating = False
With Range("A1:A100")
.Value = WorksheetFunction.Transpose(objDic.Keys())
.Sort .Cells(1)
End With
Application.ScreenUpdating = True
Set objDic = Nothing
End Sub