|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
请见代码:
- Sub lqxs()
- Dim arr, i, x$, y$, kk, tt, aa, ii%, j%, c%, n%, nn%
- Dim d, k, t, d1, d2
- Set d = CreateObject("scripting.dictionary")
- Set d1 = CreateObject("scripting.dictionary")
- Set d2 = CreateObject("scripting.dictionary")
- Sheet2.Activate
- [d:m].ClearContents: c = 4
- arr = Sheet1.[a1].CurrentRegion
- For i = 2 To UBound(arr)
- y = arr(i, 1): x = arr(i, 2)
- If Not d1.exists(x) Then c = c + 1: d1(x) = c
- If d.exists(x) = False Then Set d(x) = CreateObject("scripting.dictionary")
- d(x)(y) = d(x)(y) & arr(i, 3) & ","
- Next
- [e1].Resize(1, d1.Count) = d1.keys
- k = d.keys: t = d.items: n = 1
- For i = 0 To UBound(k)
- c = d1(k(i)): nn = 0
- kk = t(i).keys: tt = t(i).items
- For ii = 0 To UBound(kk)
- If Not d2.exists(kk(ii)) Then
- n = n + 1
- Cells(n, 4) = kk(ii)
- d2(kk(ii)) = d2(kk(ii)) & n & ","
- Else
- t2 = d2(kk(ii))
- t2 = Left(t2, Len(t2) - 1)
- If InStr(t2, ",") Then
- aa = Split(t2, ",")
- For j = 0 To UBound(aa)
- If Cells(Val(aa(j)), c) = "" Then nn = Val(aa(j)): Exit For
- Next
- Else
- nn = Val(t2)
- End If
- End If
- tt(ii) = Left(tt(ii), Len(tt(ii)) - 1)
- If InStr(tt(ii), ",") Then
- aa = Split(tt(ii), ",")
- For j = 0 To UBound(aa)
- If j > 0 Then
- If nn = 0 Then
- n = n + 1
- Cells(n, 4) = kk(ii)
- d2(kk(ii)) = d2(kk(ii)) & n & ","
- End If
- End If
- If nn = 0 Then
- Cells(n, c) = aa(j)
- Else
- Cells(nn, c) = aa(j)
- If j <> UBound(aa) Then nn = nn + 1 Else nn = 0
- End If
- Next
- Else
- If nn = 0 Then Cells(n, c) = tt(ii) Else Cells(nn, c) = tt(ii): nn = 0
- End If
- Next
- Next
- End Sub
复制代码 |
|