重新学习了几天,结果还是无法彻底搞清楚递归怎么才合理;exit退出后居然还调运自身运行了个循环练习练习再练习
- Dim d, ar, r&, c&, n&, a(9) As String
- Sub tt()
- Set d = CreateObject("scripting.dictionary")
- arr = [a1].CurrentRegion
- ReDim ar(UBound(arr), 9) '推荐人存入字典
- For i% = 2 To UBound(arr): d(arr(i, 1)) = d(arr(i, 1)) & " " & arr(i, 2): Next
- all$ = Join(d.items) & " "
- For Each w In d.keys '对每个一级推荐人递归循环
- If InStr(all, " " & w & " ") = 0 Then
- ar(n, 0) = w: a(1) = Mid(d(w), 2)
- Call dg(a, n, 1)
- End If
- Next
- [f3].Resize(n + 1, 9) = ar
- End
- End Sub
- Sub dg(a, r, c)
- x = Split(a(c) & " ")(0): ar(r, c) = x
- If d.exists(x) Then '向后递增
- a(c + 1) = Mid(d(x), 2)
- Call dg(a, r, c + 1)
- ElseIf InStr(a(c), " ") Then '最后一级
- a(c) = Replace(a(c), x & " ", "")
- Call dg(a, r + 1, c)
- Else '向前返回
- a(c - 1) = Mid(a(c - 1), InStr(a(c - 1) & " ", " ") + 1)
- If Len(a(c - 1)) Then
- Call dg(a, r + 1, c - 1)
- Else
- n = r + 1
- Exit Sub '退出后,end if执行,不懂
- End If
- End If
- End Sub
复制代码
|