如何有效缩小Excel文件大小提升工作效率与存储空间
972
2022-11-29
Excel三个vba生成不重复随机整数的案例
文章提供三个vba代码生成不重复随机数的案例供学习。
vba不重复随机数案例一:下面的代码,将在A1:A5000单元格产生1-5000的不重复随机整数。Private Sub vba不重复随机数()Dim Arr1(1 To 5000) As BooleanDim Arr2(1 To 5000, 0) As Integert = Timer[A:A] = EmptyFor i = 1 To 5000num = Int(5000 * Rnd + 1)Do While Arr1(num)num = Int(5000 * Rnd + 1)LoopArr1(num) = TrueArr2(i, 0) = numNext i[A1:A5000] = Arr2End Sub
vba不重复随机数案例二:在A1:A100中产生1-100的不重复随机整数,代码如下:Sub vba不重复随机数()Dim TempArr1(99) As Integer, TempArr2(0 To 99, 1 To 1) As IntegerDim RndNumber As Integer, i As IntegerRandomize (Timer) ‘初始化随机数生成器For i = 0 To 99 ‘产生包含1-100的不重复的随机数列TempArr1(i) = iNext iFor i = 99 To 0 Step -1RndNumber = Int(i * Rnd)TempArr2(99 – i, 1) = TempArr1(RndNumber) + 1TempArr1(RndNumber) = TempArr1(i)Next i‘在A1:A100中输入这些数字Range("a1:a100").Value = TempArr2End Sub
vba不重复随机数案例三:在A1:A10单元格产生10个0-100的随机整数,以下是代码:Sub vba不重复随机数()Dim i As Longi = 100Dim num As LongReDim arr(i) As LongReDim Arr2(i, 0) As LongDim x As LongFor x = 0 To iarr(x) = xNext xFor x = 0 To inum = Int(Rnd() * (i – x))Arr2(x, 0) = arr(num)arr(num) = arr(i – x)Next xRange("A1:A10") = Arr2End Sub
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。