|
楼主 |
发表于 2024-6-12 21:20
|
显示全部楼层
下面代码是从论坛的网友复制来的,可以参考
Sub main()
Dim Ar, I&, Br(), K&
Ar = Range("a1:a" & Cells(Rows.Count, 1).End(3).Row).Value
For I = 1 To UBound(Ar)
If Ar(I, 1) <> "" Then
K = K + 1
ReDim Preserve Br(1 To K)
Br(K) = Ar(I, 1)
End If
Next I
Call GetRnd(Br, 1, K, K)
K = 0
For I = 1 To UBound(Ar)
If Ar(I, 1) <> "" Then
K = K + 1
Ar(I, 1) = Br(K)
End If
Next I
Range("b1").Resize(UBound(Ar), 1) = Ar
End Sub
Sub GetRnd(Brr, a, b, N&) '香川
Dim L&, I&, R&, T
Randomize
L = LBound(Brr)
For I = L To N + L - 1 '一維数組洗牌
R = Int(Rnd() * (b - a + 1 - (I - L))) + a + (I - L)
T = Brr(R): Brr(R) = Brr(a + (I - L)): Brr(I) = T '一維
Next
End Sub |
|