sub test()
'批量随机数(行号)
dim arr
arr = get_rand(20, 19)
Sheets("sheet1").Cells(1, 1).Resize(UBound(arr), UBound(arr, 2)) = arr
end sub
'取随机数
'参数: 最大行号,随机行号数量
Function get_rand(maxno, rndnum)
Dim i, x, dict, arr
If rndnum >= maxno Then MsgBox "随机数超过最大数量了": Exit Function
Set dict = CreateObject("scripting.dictionary")
ReDim arr(1 To rndnum, 1 To 1)
Randomize
For i = 1 To UBound(arr)
'模板名称,变量名,不要用rnd作为变量名称
x = i
Do While True
x = Int(rnd * maxno) + 1 'rnd:0-1,行号1-n
If x <> 1 And Not dict.exists(x) Then '不为标题1
dict.Add x, 1
Exit Do
End If
Loop
arr(i, 1) = x
Next
Set dict = Nothing
get_rand = arr
End Function
VBA批量取随机数(行号)
最新推荐文章于 2024-04-01 09:56:26 发布