Dim Buf_rnd As Long '用來暫時存放亂數值,等比對沒有重複後才存入儲存格 Dim i As Long Dim j As Long
If rnd_end - rnd_start < cell_end - cell_start Then MsgBox "產生不重複的亂數區間不可以比要產生亂數的儲存格的數值還要少。" Exit Sub End If
'產生亂數 For i = cell_start To cell_end 1 Buf_rnd = Int(rnd() * (rnd_end - rnd_start + 1)) + rnd_start '如果不是第1個儲存格就要往前比對。 If i > cell_start Then For j = cell_start To i - 1 '如果重複了就跳到 1 重新產生亂數。 If Buf_rnd = Range(Mycol & j).Value Then GoTo 1 Next j End If Range(Mycol & i).Value = Buf_rnd Next i