'不一定正确,特别是关系式。你这数据量太小了
Option Explicit
Const FINDSTR As String = "a"
Sub test()
Dim i, j, arr, dic, cnt
Set dic = CreateObject("scripting.dictionary")
arr = [a3:e6]
ReDim brr(1 To UBound(arr, 1) * 2, 1 To 3)
For i = 1 To UBound(arr, 1)
brr(i, 1) = arr(i, 1): brr(i, 2) = arr(i, 2): brr(i, 3) = arr(i, 3)
brr(UBound(arr, 1) + i, 1) = arr(i, 1)
brr(UBound(arr, 1) + i, 2) = arr(i, 3)
brr(UBound(arr, 1) + i, 3) = arr(i, 4)
Next
For i = 1 To UBound(brr, 1)
If brr(i, 2) = FINDSTR Then Exit For
Next
If i = UBound(brr, 1) + 1 Then MsgBox "!": Exit Sub
dic(brr(i, 1)) = brr(i, 2) & "->" & brr(i, 3): brr(i, 2) = vbNullString
Call dfs(brr, brr(i, 3), dic)
For i = 1 To UBound(arr, 1)
If dic.exists(arr(i, 1)) Then
cnt = cnt + 1
For j = 1 To UBound(arr, 2) - 1: arr(cnt, j) = arr(i, j): Next
arr(cnt, j) = dic(arr(i, 1))
End If
Next
With [g2] '输出位置自己修改
.Resize(Rows.Count - 1, UBound(arr, 2)).ClearContents
.Resize(cnt, UBound(arr, 2)) = arr
End With
End Sub
Function dfs(arr, s, dic)
Dim i
For i = 1 To UBound(arr, 1)
If Len(arr(i, 2)) Then
If arr(i, 2) = s Then
If dic.exists(arr(i, 1)) Then
dic(arr(i, 1)) = dic(arr(i, 1)) & "->" & arr(i, 3)
Else
dic(arr(i, 1)) = arr(i, 2) & "->" & arr(i, 3)
End If
Call dfs(arr, arr(i, 3), dic)
End If
End If
Next
End Function |