|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
- Sub test()
- Dim r%, i%
- Dim arr, brr
- Dim d As Object
- Set d = CreateObject("scripting.dictionary")
- With Worksheets("原始数据")
- r = .Cells(.Rows.Count, 1).End(xlUp).Row
- arr = .Range("a2:e" & r)
- End With
- For i = 1 To UBound(arr)
- If Not d.exists(arr(i, 1)) Then
- Set d(arr(i, 1)) = CreateObject("scripting.dictionary")
- End If
- If Not d(arr(i, 1)).exists(arr(i, 2)) Then
- Set d(arr(i, 1))(arr(i, 2)) = CreateObject("scripting.dictionary")
- End If
- For j = 4 To 5
- If Not d(arr(i, 1))(arr(i, 2)).exists(j) Then
- Set d(arr(i, 1))(arr(i, 2))(j) = CreateObject("scripting.dictionary")
- End If
- arr(i, j) = Replace(arr(i, j), " ", "")
- brr = Split(arr(i, j), ",")
- For k = 0 To UBound(brr)
- If Not d(arr(i, 1))(arr(i, 2))(j).exists(brr(k)) Then
- Set d(arr(i, 1))(arr(i, 2))(j)(brr(k)) = CreateObject("scripting.dictionary")
- End If
- d(arr(i, 1))(arr(i, 2))(j)(brr(k))(arr(i, 3)) = ""
- Next
- Next
- Next
- With Worksheets("结果")
- .UsedRange.Offset(1, 0).ClearContents
- m = 2
- For Each aa In d.keys
- For Each bb In d(aa).keys
- For k = 4 To 5
- kk = d(aa)(bb)(k).keys
- For i = 0 To UBound(kk) - 1
- p = i
- For j = i + 1 To UBound(kk)
- If d(aa)(bb)(k)(kk(p)).Count < d(aa)(bb)(k)(kk(j)).Count Then
- p = j
- End If
- Next
- If p <> i Then
- temp = kk(i)
- kk(i) = kk(p)
- kk(p) = temp
- End If
- Next
- For i = 0 To Application.Min(2, UBound(kk))
- .Cells(m + i, k * 5 - 19) = aa
- .Cells(m + i, k * 5 - 19 + 1) = bb
- .Cells(m + i, k * 5 - 19 + 2) = kk(i)
- .Cells(m + i, k * 5 - 19 + 3) = d(aa)(bb)(k)(kk(i)).Count
- .Cells(m + i, k * 5 - 19 + 4) = Join(d(aa)(bb)(k)(kk(i)).keys, ",")
- Next
- Next
- m = m + 4
- Next
- Next
- End With
- End Sub
复制代码 |
评分
-
2
查看全部评分
-
|