VBA批量取随机数(行号)

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值