|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
Option Explicit
Sub test()
Dim dic(1), i, t, arr, m, n, s, key
For i = 0 To UBound(dic)
Set dic(i) = CreateObject("scripting.dictionary")
Next
arr = [a1].CurrentRegion
For i = 2 To UBound(arr, 1)
If dic(0).exists(arr(i, 1)) Then
t = dic(0)(arr(i, 1))
ReDim Preserve t(UBound(t) + 1)
t(UBound(t)) = arr(i, 3)
dic(0)(arr(i, 1)) = t
Else
dic(0)(arr(i, 1)) = Array(arr(i, 3))
dic(1)(arr(i, 1)) = Array(arr(i, 2), arr(i, 4))
End If
Next
ReDim brr(1 To dic(0).Count * 2, 1 To 4) As String
Randomize
For Each key In dic(0).keys
t = dic(0)(key)
For i = 0 To UBound(t)
m = Int(Rnd * (UBound(t) + 1))
s = t(i): t(i) = t(m): t(m) = s
Next
s = dic(1)(key)
n = n + 1
brr(n, 1) = key: brr(n, 3) = t(0)
brr(n, 2) = s(0): brr(n, 4) = s(1)
If UBound(t) > 0 Then
n = n + 1
brr(n, 1) = key: brr(n, 3) = t(1)
brr(n, 2) = s(0): brr(n, 4) = s(1)
End If
Next
With [f2]
.Resize(Rows.Count - 1, UBound(brr, 2)).ClearContents
.Resize(Rows.Count - 1, UBound(brr, 2)).NumberFormatLocal = "@"
.Resize(UBound(brr, 1), UBound(brr, 2)) = brr
End With
End Sub |
|