|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
- Sub CloseLink()
- 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
- [D1].Resize(nRow) = Application.WorksheetFunction.Transpose(vData)
- Set dicData = CreateObject("Scripting.Dictionary")
- For nRow = 1 To UBound(vData)
- sLink = vData(nRow)
- sFirst = Left(sLink, 1)
- vLen = Len(sLink)
- If Not dicData.Exists(vLen) Then Set dicData(vLen) = CreateObject("Scripting.Dictionary")
- For Each vLink In dicData(vLen).Keys
- If Left(vLink, 1) <> sFirst Then
- sMid = Mid(vLink, 2, Len(vLink) - 2)
- nBit = InStr(sMid, sFirst)
- If nBit > 0 Then
- sStr = Right(vLink, Len(vLink) - nBit) & IIf(nBit > 2, Mid(vLink, 2, nBit), "") & sFirst
- bIsSame = sStr = sLink
- End If
- 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
- [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 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
复制代码 |
|