|
提供一个不限制字数的找闭环的算法予以参考
- Private vData As Variant, nRow As Long, dicData As Object
- Sub CloseLink()
- 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:D].ClearContents
- If nRow > 0 Then [D1].Resize(nRow) = Application.WorksheetFunction.Transpose(vData)
- 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 Len(sLink & vKey) > 2 Then
- nRow = nRow + 1
- ReDim Preserve vData(1 To nRow)
- vData(nRow) = sLink & vKey
- ElseIf Not (sLink Like "*" & vKey & "*") Then
- If dicData.Exists(vKey) Then GetLink dicData(vKey), IIf(sFirst = "", vKey, sFirst), sLink & vKey
- End If
- Next
- End Function
复制代码 |
|