|
Sub 不重复随机值()
Dim iMin As Integer, iMax As Integer, iCount As Integer
Dim j As Integer
j = 1
Application.ScreenUpdating = False
On Error GoTo 100
Columns("A:A").ClearContents
iMin = Int(InputBox("请输入最小值"))
iMax = Int(InputBox("请输入最大值"))
iCount = Int(InputBox("请输入要取的个数"))
If iMax <= iMin Then
MsgBox "最大值必须比最小值大!"
Exit Sub
End If
If iCount > iMax - iMin + 1 Then
MsgBox "要取出的个数,不能超过大小值之差!"
Exit Sub
End If
For i = iMin To iMax
Range("A" & j) = i
Range("B" & j) = Rnd
j = j + 1
Next i
Range("A1:B" & iMax - iMin + 1).Sort Key1:=Range("B1")
Columns("B:B").ClearContents
If iCount < iMax - iMin + 1 Then
Range(Cells(iCount + 1, 1), Cells(iMax - iMin + 1, 1)).ClearContents
End If
100
End Sub |
|