'可能是这样
Option Explicit
Sub test()
Dim i, j, k, a, n, t, key
ReDim arr(1 To 10, 1 To 10), dic(1 To UBound(arr, 1))
For i = 1 To UBound(dic)
Set dic(i) = CreateObject("scripting.dictionary")
Next
For i = 1 To UBound(arr, 2): arr(1, i) = i: Next
Randomize
For i = 1 To UBound(arr, 2)
n = Int(Rnd * UBound(arr, 1)) + 1
t = arr(1, i): arr(1, i) = arr(1, n): arr(1, n) = t
Next
For j = 1 To UBound(arr, 2)
dic(1).RemoveAll: n = 1
For i = 1 To UBound(arr, 1)
If i <> arr(1, j) Then dic(1)(i) = vbNullString
Next
For Each key In dic(1).keys
n = n + 1: arr(n, j) = key
Next key, j
For j = 1 To UBound(arr, 2)
Randomize
For i = 2 To UBound(arr, 1)
n = Int(Rnd * (UBound(arr, 2) - 1) + 2)
t = arr(i, j): arr(i, j) = arr(n, j): arr(n, j) = t
Next
For i = 2 To UBound(arr, 1)
If dic(i).exists(arr(i, j)) Then Exit For
Next
If i = UBound(arr, 1) + 1 Then
For i = 2 To UBound(arr, 1)
dic(i)(arr(i, j)) = vbNullString
Next
Else
j = j - 1
End If
Next
For i = 1 To UBound(arr, 1)
For j = 1 To UBound(arr, 2)
arr(i, j) = arr(i, j)
Next j, i
[a1].Resize(UBound(arr, 1), UBound(arr, 2)) = arr
End Sub |