?如果我们要用VBA生成一列不重复的随机整数,可以用下面的VBA代码。本例可以在A1:A100中产生1-100的不重复随机整数。
Sub RndNumberNoRepeat()
Dim TempArr1(99) As Integer, TempArr2(0 To 99, 1 To 1) As Integer
Dim RndNumber As Integer, i As Integer
Randomize (Timer) '初始化随机数生成器
For i = 0 To 99 '产生包含1-100的不重复的随机数列
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中输入这些数字
Range("a1:a100").Value = TempArr2
End Sub
……