|
东拼西凑,终于写出一个,能得到结果,不过感觉需优化的东西太多- Option Explicit
- Dim d, aa, n%, jg(), x%
- Sub qq()
- Dim i&, arr, j%, z$, z1, d1, r0%, j1%, y%, m%, t$
- i = [b65536].End(xlUp).Row
- arr = Range("b2:d" & i)
- Set d = CreateObject("Scripting.Dictionary")
- Set d1 = CreateObject("Scripting.Dictionary")
- Range("g2:h100").ClearContents
- For i = 1 To UBound(arr)
- If arr(i, 3) = "无" Then
- Set d(arr(i, 1)) = CreateObject("Scripting.Dictionary")
- End If
- Next
- For Each aa In d.keys
- x = 0: Erase jg: d1.RemoveAll
- For i = 1 To UBound(arr)
- If arr(i, 3) <> "无" Then
- If Not d.exists(arr(i, 3)) Then
- Set d(arr(i, 3)) = CreateObject("Scripting.Dictionary")
- End If
- d(arr(i, 3))(arr(i, 1)) = ""
- End If
- Next
- m = d(aa).Count
- If m = 0 Then
- r0 = [g65536].End(xlUp).Row
- Cells(r0 + 1, "g") = aa & "(1级)"
- Cells(r0 + 1, "h") = 1
- Else
- Call digui(aa, 1, "")
- r0 = [g65536].End(xlUp).Row
- z = Join(jg, ",")
- z1 = Split(z, ",")
- For j = 0 To UBound(z1)
- If Not d1.exists(z1(j)) Then d1(z1(j)) = ""
- Next
- For j = 0 To d1.Count - 1
- z1 = Split(d1.keys()(j), "(")
- x = Val(z1(1))
- Cells(r0 + 1 + j, "g").IndentLevel = x - 1
- Cells(r0 + 1 + j, "g") = d1.keys()(j)
- Next
- For j = r0 + 1 To [g65536].End(xlUp).Row
- For j1 = j + 1 To [g65536].End(xlUp).Row + 1
- If Cells(j1, "g") = "" Then
- Exit For
- Else
- If Cells(j1, "g").IndentLevel > Cells(j, "g").IndentLevel Then
- y = y + 1
- Else
- Exit For
- End If
- End If
- Next
- Cells(j, "h") = y + 1: y = 0
- Next
- End If
- Next
- End Sub
- Sub digui(Pid, n, s)
- Dim i&, key, temp2
- If d.exists(Pid) Then
- temp2 = s & Pid & "(" & n & "级" & ")" & ","
- key = d(Pid).keys
- For i = 0 To UBound(key)
- If InStr(temp2, key(i) & "(" & n & "级" & ")" & ",") = 0 Then Call digui(key(i), n + 1, temp2)
- Next
- Else
- x = x + 1
- ReDim Preserve jg(1 To x)
- jg(x) = s & Pid & "(" & n & "级" & ")"
- End If
- End Sub
复制代码 |
|