|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
- Sub test()
- Dim dic, data, cc, i&, d, arr(), k&, tmp, j&
- With ThisWorkbook.Worksheets("Sheet1")
- data = .[A1:B16]
- cc = .[M1:M4]
- End With
- Set dic = CreateObject("Scripting.Dictionary")
- For i = 2 To UBound(data)
- If data(i, 1) <> "" Then
- If dic(data(i, 1) & "") = "" Then
- dic(data(i, 1) & "") = data(i, 2)
- Else
- dic(data(i, 1) & "") = dic(data(i, 1) & "") & "|" & data(i, 2)
- End If
- End If
- Next i
- For Each d In dic.keys
- Call dg(d, dic, "")
- Next d
- For i = 2 To UBound(cc)
- If cc(i, 1) <> "" Then
- k = k + 1
- ReDim Preserve arr(1 To 3, 1 To k)
- arr(1, k) = cc(i, 1)
- If dic.exists(cc(i, 1) & "") Then
- tmp = Split(dic(cc(i, 1) & ""), "|")
- arr(2, k) = UBound(tmp) + 1
- arr(3, k) = tmp(0)
- For j = 1 To UBound(tmp)
- k = k + 1
- ReDim Preserve arr(1 To 3, 1 To k)
- arr(3, k) = tmp(j)
- Next j
- End If
- End If
- Next i
- With ThisWorkbook.Worksheets("Sheet1")
- .[R2].Resize(k, 3) = Application.Transpose(arr)
- End With
- End Sub
- Sub dg(x, dic, ss)
- Dim tmp, i&
- tmp = Split(dic(x), "|")
- For i = 0 To UBound(tmp)
- If InStr(ss, tmp(i)) = 0 Then
- If ss = "" Then ss = tmp(i) Else ss = ss & tmp(i)
- If dic.exists(tmp(i)) Then
- dic(x) = dic(x) & "|" & dic(tmp(i))
- Call dg(x, dic, ss)
- End If
- End If
- Next i
- End Sub
复制代码 |
|