|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
点击表中的箭头 但没有按你的顺序排序
- Public Sub 树转父子()
- Dim dicA
- Dim dicB '用来去重
- Dim Arr, i&, j&, X&, Xrr(1 To 1000, 1 To 4)
- Set dicA = CreateObject("Scripting.Dictionary")
- Set dicB = CreateObject("Scripting.Dictionary")
- Arr = Sheets("Sheet1").Range("C1").CurrentRegion
- For i = 2 To UBound(Arr)
- dicA(0) = Arr(i, 1) '顶层
- dicA(Arr(i, 2)) = Arr(i, 3)
- s = dicA(Arr(i, 2) - 1) & "->" & Arr(i, 3)
- X = Arr(i, 4)
- If Arr(i, 2) = 1 Then
- X = Arr(i, 4)
- ElseIf Arr(i, 2) - Arr(i - 1, 2) = 1 Then
- X = X / Arr(i - 1, 4)
- ElseIf Arr(i, 2) <= Arr(i - 1, 2) Then
- For j = i To 2 Step -1
- If Arr(i, 2) - Arr(j, 2) = 1 Then X = X / Arr(j, 4): Exit For
- Next
- End If
- dicB(s) = Array(dicA(Arr(i, 2) - 1), Arr(i, 3), X)
- ' dicB(s) = Array(dicA(Arr(i, 2) - 1), Arr(i, 3), X, Arr(i, 2))
- Next i
- it = dicB.Items
- a = Application.Transpose(it)
- b = Application.Transpose(a)
- Range("P2").CurrentRegion.Offset(1).ClearContents
- Range("P2").Resize(UBound(b), UBound(b, 2)) = b
- End Sub
复制代码
|
|