我正在尝试生成n个随机数字,除了这个数字(81-82-83-84-85-86)以外,其他没有重复。
在此VBA代码b(1至80)中,我想对b(1至200)进行重复编号(81-82-83-84-85-86),并且number_required是其中的行数每次我更改一栏:
Dim b() As Boolean, Bidder_ID As Range, k&, x&
ReDim b(1 To 80)
Dim destination_Bidder_ID As String
Randomize
Number_required = Range("K2").Value
destination_Bidder_ID = Range("L3").Value
Range(destination_Bidder_ID).Select
For Each Bidder_ID In Range(Selection, Selection.Offset(Number_required - 1, 0))
Do
x = Int(Rnd() * 80) + 1
If b(x) = False Then
Bidder_ID.Value = x
b(x) = True
Exit Do
End If
k = k + 1: If k > 100 Then Exit Sub
Loop
Next
我如何生成1到200之间的随机数,除了已经在定义范围内的该数字(81-82-83-84-85-86)?
答案 0 :(得分:1)
尝试一下
Sub RndBtwn_Excldng()
Application.ScreenUpdating = False
Dim c As Range, rng As Range, i As Integer
Set rng = Range([L3]).Resize([K2])
For Each c In rng
Select Case Int(rnd * 10) Mod 2
Case 0: lb = 1: ub = 80
Case 1: lb = 87: ub = 200
End Select
i = Int((ub - lb + 1) * rnd + lb)
Do While Application.WorksheetFunction.CountIf(rng, i) >= 1
i = Int((ub - lb + 1) * rnd + lb)
Loop
c = i
Next
Application.ScreenUpdating = True
End Sub