我工作的隨機數VBA和下面的代碼從挑選和Sheet2的副本名稱表1,我可以重複這些步驟來接從該列表3個不同的名稱。如果三個連續的行都是三個連續的行,但這些代碼在第5行,第10行和第15行中沒有選擇3個不同的名稱,那麼這個代碼做得很好。有人可以幫我挑三個不同的名字,並將它們放在相距5行的行中嗎? (第一行第一名,第五行第二名,第十五行第三名)我是VBA新手!輸出隨機數在三個不同的行
Sub DDQ1()
Application.ScreenUpdating = False
Dim source, destination As Range
Set source = Sheets("sheet2").Range("A60:A81")
Sheets("sheet1").Activate
Set destination = ActiveSheet.Range("B53")
ReDim randoms(1 To source.Rows.Count)
destrow = 0
For i = 1 To destination.Rows.Count
If destination(i) = "" Then: destrow = i: Exit For
Next i
If destrow = 0 Then: MsgBox "no more room in destination range": Exit Sub
For i = 1 To UBound(randoms): randoms(i) = Rnd(): Next i
ipick = 0: tries = 0
Do While ipick = 0 And tries < UBound(randoms)
tries = tries + 1
minrnd = WorksheetFunction.Min(randoms)
For i = 1 To UBound(randoms)
If randoms(i) = minrnd Then
picked_before = False
For j = 1 To destrow - 1
If source(i) = destination(j) Then: picked_before = True: randoms(i) = 2: Exit For
Next j
If Not picked_before Then: ipick = i
Exit For
End If
Next i
Loop
If ipick = 0 Then: MsgBox "no more unique name possible to pick": Exit Sub
destination(destrow) = source(ipick)
Application.ScreenUpdating = True
End Sub
不回答你的問題,而是'點心源,目的地Range'將目的地的範圍內,但消息人士的變種。你需要'Dim source作爲Range,destination As Range' – jcarroll
這裏有一點小小的格式化。 – RBarryYoung