ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 效率神器,一键搞定繁琐工作
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
让更多数据处理,一键完成 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
楼主: mrgood88

[求助] 根据上级关系生成由高到低层级关系

[复制链接]

TA的精华主题

TA的得分主题

发表于 2018-9-5 17:24 | 显示全部楼层
根据上级关系生成由高到低层级关系.rar (16.81 KB, 下载次数: 24)

TA的精华主题

TA的得分主题

发表于 2018-9-5 18:06 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
完整版,含人数计算: 根据上级关系生成由高到低层级关系.rar (19.74 KB, 下载次数: 135)

评分

1

查看全部评分

TA的精华主题

TA的得分主题

 楼主| 发表于 2018-9-5 19:46 来自手机 | 显示全部楼层
谢谢各位大侠,试验了下,当层关系太多7.8级时,这样显示效果也不直观,哪位高手能做成树状折叠或分级显示

TA的精华主题

TA的得分主题

发表于 2018-9-6 09:36 | 显示全部楼层
lsdongjh 发表于 2018-9-5 18:06
完整版,含人数计算:

老师你好,能否把递归部分改成SUB过程,我试着改了一下没成功,谢谢你

TA的精华主题

TA的得分主题

发表于 2018-9-6 12:40 | 显示全部楼层
ykqrs 发表于 2018-9-6 09:36
老师你好,能否把递归部分改成SUB过程,我试着改了一下没成功,谢谢你

sub 不能回参。

评分

1

查看全部评分

TA的精华主题

TA的得分主题

发表于 2018-9-6 18:59 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助

感谢你的回复,你的代码留待以后慢慢研究吧,我重新把我那个优化了一下,结果和你的一样。
  1. Dim d, aa, x%, str$
  2. Sub zz()
  3. Dim i&, arr, j%, z1, r0%, j1%, y%
  4. i = [b65536].End(xlUp).Row
  5. arr = Range("b2:d" & i)
  6. Set d = CreateObject("Scripting.Dictionary")
  7. Range("g2:h100").ClearContents
  8. For i = 1 To UBound(arr)
  9. If Not d.exists(arr(i, 3)) Then
  10. Set d(arr(i, 3)) = CreateObject("Scripting.Dictionary")
  11. End If
  12. d(arr(i, 3))(arr(i, 1)) = "(" & arr(i, 2) & ")"
  13. Next
  14. For Each aa In d("无").keys
  15. x = 0: str = ""
  16. If Not d.exists(aa) Then
  17. r0 = [g65536].End(xlUp).Row
  18. Cells(r0 + 1, "g") = aa & d("无")(aa)
  19. Cells(r0 + 1, "h") = 1
  20. Else
  21. Call digui(aa, 0, "", d("无")(aa))
  22. r0 = [g65536].End(xlUp).Row
  23. z1 = Split(str, ",")
  24. For j = 0 To UBound(z1)
  25. Cells(r0 + 1 + j, "g").IndentLevel = Val(Split(z1(j), "|")(1))
  26. Cells(r0 + 1 + j, "g") = Split(z1(j), "|")(0)
  27. Next
  28. For j = 0 To UBound(z1) - 1
  29. For j1 = j + 1 To UBound(z1)
  30. If Val(Split(z1(j1), "|")(1)) > Val(Split(z1(j), "|")(1)) Then
  31. y = y + 1
  32. Else
  33. Exit For
  34. End If
  35. Next
  36. Cells(r0 + 1 + j, "h") = y + 1: y = 0
  37. Next
  38. Cells(r0 + 1 + j, "h") = y + 1: y = 0
  39. End If
  40. Next
  41. End Sub
  42. Sub digui(Pid, n, s, w)
  43. Dim i&, key, temp2, sr%, sar
  44. If d.exists(Pid) Then
  45. temp2 = s & Pid & w & "|" & n & ","
  46. key = d(Pid).keys
  47. For i = 0 To UBound(key)
  48. Call digui(key(i), n + 1, temp2, d(Pid)(key(i)))
  49. Next
  50. Else
  51. If str = "" Then
  52. str = s & Pid & w & "|" & n
  53. Else
  54. sar = Split(s & Pid & w & "|" & n & ",", ",")
  55. For sr = 0 To UBound(sar)
  56. If InStr(str, sar(sr)) = 0 Then str = str & "," & sar(sr)
  57. Next
  58. End If
  59. End If
  60. End Sub
复制代码

TA的精华主题

TA的得分主题

发表于 2018-9-6 19:01 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
怎么回事?发一个贴一审核吗?谁能告诉我怎样才不用审核吗?

TA的精华主题

TA的得分主题

发表于 2018-9-6 19:02 | 显示全部楼层
Dim d, aa, x%, str$
Sub zz()
Dim i&, arr, j%, z1, r0%, j1%, y%
i = [b65536].End(xlUp).Row
arr = Range("b2:d" & i)
Set d = CreateObject("Scripting.Dictionary")
Range("g2:h100").ClearContents
For i = 1 To UBound(arr)
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)) = "(" & arr(i, 2) & ")"
Next
For Each aa In d("无").keys
x = 0: str = ""
If Not d.exists(aa) Then
r0 = [g65536].End(xlUp).Row
Cells(r0 + 1, "g") = aa & d("无")(aa)
Cells(r0 + 1, "h") = 1
Else
Call digui(aa, 0, "", d("无")(aa))
r0 = [g65536].End(xlUp).Row
z1 = Split(str, ",")
For j = 0 To UBound(z1)
Cells(r0 + 1 + j, "g").IndentLevel = Val(Split(z1(j), "|")(1))
Cells(r0 + 1 + j, "g") = Split(z1(j), "|")(0)
Next
For j = 0 To UBound(z1) - 1
For j1 = j + 1 To UBound(z1)
If Val(Split(z1(j1), "|")(1)) > Val(Split(z1(j), "|")(1)) Then
y = y + 1
Else
Exit For
End If
Next
Cells(r0 + 1 + j, "h") = y + 1: y = 0
Next
Cells(r0 + 1 + j, "h") = y + 1: y = 0
End If
Next
End Sub
Sub digui(Pid, n, s, w)
Dim i&, key, temp2, sr%, sar
If d.exists(Pid) Then
temp2 = s & Pid & w & "|" & n & ","
key = d(Pid).keys
For i = 0 To UBound(key)
Call digui(key(i), n + 1, temp2, d(Pid)(key(i)))
Next
Else
If str = "" Then
str = s & Pid & w & "|" & n
Else
sar = Split(s & Pid & w & "|" & n & ",", ",")
For sr = 0 To UBound(sar)
If InStr(str, sar(sr)) = 0 Then str = str & "," & sar(sr)
Next
End If
End If
End Sub

TA的精华主题

TA的得分主题

发表于 2018-9-6 19:02 | 显示全部楼层
  1. Dim d, aa, x%, str$
  2. Sub zz()
  3. Dim i&, arr, j%, z1, r0%, j1%, y%
  4. i = [b65536].End(xlUp).Row
  5. arr = Range("b2:d" & i)
  6. Set d = CreateObject("Scripting.Dictionary")
  7. Range("g2:h100").ClearContents
  8. For i = 1 To UBound(arr)
  9. If Not d.exists(arr(i, 3)) Then
  10. Set d(arr(i, 3)) = CreateObject("Scripting.Dictionary")
  11. End If
  12. d(arr(i, 3))(arr(i, 1)) = "(" & arr(i, 2) & ")"
  13. Next
  14. For Each aa In d("无").keys
  15. x = 0: str = ""
  16. If Not d.exists(aa) Then
  17. r0 = [g65536].End(xlUp).Row
  18. Cells(r0 + 1, "g") = aa & d("无")(aa)
  19. Cells(r0 + 1, "h") = 1
  20. Else
  21. Call digui(aa, 0, "", d("无")(aa))
  22. r0 = [g65536].End(xlUp).Row
  23. z1 = Split(str, ",")
  24. For j = 0 To UBound(z1)
  25. Cells(r0 + 1 + j, "g").IndentLevel = Val(Split(z1(j), "|")(1))
  26. Cells(r0 + 1 + j, "g") = Split(z1(j), "|")(0)
  27. Next
  28. For j = 0 To UBound(z1) - 1
  29. For j1 = j + 1 To UBound(z1)
  30. If Val(Split(z1(j1), "|")(1)) > Val(Split(z1(j), "|")(1)) Then
  31. y = y + 1
  32. Else
  33. Exit For
  34. End If
  35. Next
  36. Cells(r0 + 1 + j, "h") = y + 1: y = 0
  37. Next
  38. Cells(r0 + 1 + j, "h") = y + 1: y = 0
  39. End If
  40. Next
  41. End Sub
  42. Sub digui(Pid, n, s, w)
  43. Dim i&, key, temp2, sr%, sar
  44. If d.exists(Pid) Then
  45. temp2 = s & Pid & w & "|" & n & ","
  46. key = d(Pid).keys
  47. For i = 0 To UBound(key)
  48. Call digui(key(i), n + 1, temp2, d(Pid)(key(i)))
  49. Next
  50. Else
  51. If str = "" Then
  52. str = s & Pid & w & "|" & n
  53. Else
  54. sar = Split(s & Pid & w & "|" & n & ",", ",")
  55. For sr = 0 To UBound(sar)
  56. If InStr(str, sar(sr)) = 0 Then str = str & "," & sar(sr)
  57. Next
  58. End If
  59. End If
  60. End Sub
复制代码

TA的精华主题

TA的得分主题

发表于 2018-9-6 19:03 | 显示全部楼层
Dim d, aa, x%, str$
Sub zz()
Dim i&, arr, j%, z1, r0%, j1%, y%
i = [b65536].End(xlUp).Row
arr = Range("b2:d" & i)
Set d = CreateObject("Scripting.Dictionary")
Range("g2:h100").ClearContents
For i = 1 To UBound(arr)
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)) = "(" & arr(i, 2) & ")"
Next
For Each aa In d("无").keys
x = 0: str = ""
If Not d.exists(aa) Then
r0 = [g65536].End(xlUp).Row
Cells(r0 + 1, "g") = aa & d("无")(aa)
Cells(r0 + 1, "h") = 1
Else
Call digui(aa, 0, "", d("无")(aa))
r0 = [g65536].End(xlUp).Row
z1 = Split(str, ",")
For j = 0 To UBound(z1)
Cells(r0 + 1 + j, "g").IndentLevel = Val(Split(z1(j), "|")(1))
Cells(r0 + 1 + j, "g") = Split(z1(j), "|")(0)
Next
For j = 0 To UBound(z1) - 1
For j1 = j + 1 To UBound(z1)
If Val(Split(z1(j1), "|")(1)) > Val(Split(z1(j), "|")(1)) Then
y = y + 1
Else
Exit For
End If
Next
Cells(r0 + 1 + j, "h") = y + 1: y = 0
Next
Cells(r0 + 1 + j, "h") = y + 1: y = 0
End If
Next
End Sub
Sub digui(Pid, n, s, w)
Dim i&, key, temp2, sr%, sar
If d.exists(Pid) Then
temp2 = s & Pid & w & "|" & n & ","
key = d(Pid).keys
For i = 0 To UBound(key)
Call digui(key(i), n + 1, temp2, d(Pid)(key(i)))
Next
Else
If str = "" Then
str = s & Pid & w & "|" & n
Else
sar = Split(s & Pid & w & "|" & n & ",", ",")
For sr = 0 To UBound(sar)
If InStr(str, sar(sr)) = 0 Then str = str & "," & sar(sr)
Next
End If
End If
End Sub
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2025-1-13 15:41 , Processed in 0.025080 second(s), 7 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表