|
这个应该能满足你的需求了
- Sub CloseLink()
- Dim vSplit As Variant, vSplitLink As Variant
- Dim vLen As Variant, sLink As String, vLink As Variant
- Dim sFirst As String, nBit As Long, sMid As String, bIsSame As Boolean, sStr As String
- Dim nI As Long
-
- With [A1].CurrentRegion
- vData = .Offset(1).Resize(.Rows.Count - 1).Value
- End With
- Set dicData = CreateObject("Scripting.Dictionary")
- For nRow = 1 To UBound(vData)
- If vData(nRow, 1) <> "" And vData(nRow, 2) <> "" Then
- If Not dicData.Exists(vData(nRow, 1)) Then Set dicData(vData(nRow, 1)) = CreateObject("Scripting.Dictionary")
- dicData(vData(nRow, 1))(vData(nRow, 2)) = ""
- End If
- Next
- ReDim vData(0)
- nRow = 0
-
- GetLink dicData
- [D:E].ClearContents
- If nRow > 0 Then
- vSplit = vData
- For nRow = 1 To UBound(vSplit)
- vSplit(nRow) = vSplit(nRow) & "∮"
- Next
- [D1].Resize(UBound(vSplit)) = Application.WorksheetFunction.Transpose(vSplit)
- Set dicData = CreateObject("Scripting.Dictionary")
- For nRow = 1 To UBound(vData)
- sLink = vData(nRow)
- vSplit = Split(sLink, "→")
- sFirst = vSplit(0) & "→"
- vLen = UBound(vSplit) + 1
- If Not dicData.Exists(vLen) Then Set dicData(vLen) = CreateObject("Scripting.Dictionary")
- For Each vLink In dicData(vLen).Keys
- nBit = InStr(vLink & "→", sFirst)
- If nBit > 1 Then
- sStr = Right(vLink & "→", Len(vLink & "→") - nBit + 1) & Left(vLink, nBit - 2)
- bIsSame = sStr = sLink
- If bIsSame Then Exit For
- End If
- Next
- If Not bIsSame Then
- dicData(vLen)(sLink) = 0
- nI = nI + 1
- If nI <> nRow Then vData(nI) = vLink
- End If
- bIsSame = False
- Next
- If nI = 1 Then
- [E1] = vData(1) & "∮"
- Else
- For nRow = 1 To UBound(vData)
- vData(nRow) = vData(nRow) & "∮"
- Next
- [E1].Resize(nI) = Application.WorksheetFunction.Transpose(vData)
- End If
- End If
- End Sub
- Private Function GetLink(ByVal oDic As Object, Optional ByVal sFirst As String, Optional ByVal sLink As String)
- Dim vKey As Variant, vDataKey As Variant
-
- For Each vKey In oDic.Keys
- If vKey = sFirst And UBound(Split(sLink & "→" & vKey, "→")) > 1 Then
- nRow = nRow + 1
- ReDim Preserve vData(1 To nRow)
- vData(nRow) = sLink
- ElseIf Not ("→" & sLink & "→" Like "*→" & vKey & "→*") Then
- If dicData.Exists(vKey) Then GetLink dicData(vKey), IIf(sFirst = "", vKey, sFirst), IIf(sLink = "", "", sLink & "→") & vKey
- End If
- Next
- End Function
复制代码 |
|