我會用一個UDF()返回一個陣列:
Public Function NoRepeats(inpt As Range) As Variant
Dim ary(), nItems As Long, i As Long
Dim r As Range
nItems = inpt.Count
ReDim ary(1 To nItems)
i = 1
For Each r In inpt
ary(i) = r.Value
i = i + 1
Next r
Call Shuffle(ary)
ReDim temp(1 To nItems, 1 To 1)
For i = 1 To nItems
temp(i, 1) = ary(i)
Next i
NoRepeats = temp
End Function
Sub Shuffle(InOut() As Variant)
Dim HowMany As Long, i As Long, J As Long
Dim tempF As Double, temp As Variant
Hi = UBound(InOut)
Low = LBound(InOut)
ReDim Helper(Low To Hi) As Double
Randomize
For i = Low To Hi
Helper(i) = Rnd
Next i
J = (Hi - Low + 1) \ 2
Do While J > 0
For i = Low To Hi - J
If Helper(i) > Helper(i + J) Then
tempF = Helper(i)
Helper(i) = Helper(i + J)
Helper(i + J) = tempF
temp = InOut(i)
InOut(i) = InOut(i + J)
InOut(i + J) = temp
End If
Next i
For i = Hi - J To Low Step -1
If Helper(i) > Helper(i + J) Then
tempF = Helper(i)
Helper(i) = Helper(i + J)
Helper(i + J) = tempF
temp = InOut(i)
InOut(i) = InOut(i + J)
InOut(i + J) = temp
End If
Next i
J = J \ 2
Loop
End Sub
這已被編碼,以回報柱格式,的結果(見的temp
調光在UDF中)
注意,UDF()已在陣列的方式被輸入與按Ctrl +移+輸入,而不僅僅是輸入鍵。
*'隨機選擇細胞沒有重複'*是一個矛盾。沒有什麼是隨機的,有附加條件。 – Jeeped