Sub LinkSort()
Dim lRow As Long, m As Long, n As Long, i As Long
Dim r As Range
Dim wb As Workbook
Dim str As String, replaced As String, Lnks As String
Set ws = ActiveSheet
lRow = Cells(Rows.Count, 1).End(xlUp).Row
Set r = Range(Cells(1, 1), Cells(lRow, "N"))
With ws
.Sort.SortFields.Clear
.Sort.SortFields.Add2 Key:=Range("N2:N" & lRow) _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With .Sort
.SetRange r
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End With
Set r = Range(Cells(2, 1), Cells(lRow, 1))
While Cells(2, "N").Value <> ""
str = Cells(2, "N").Value
replaced = Replace(str, "-", "")
n = Len(str) - Len(replaced)
If n <= 1 Then
m = Application.Match(Cells(2, "N").Value, r, 0) + 2
Rows("2:2").Cut
Rows(m & ":" & m).Insert Shift:=xlDown
If m > r.Rows.Count Then
Set r = Range(Cells(2, 1), Cells(Cells(Rows.Count, 1).End(xlUp).Row, 1))
End If
Else
Rows("2:2").Copy
Rows("3:" & 3 + n - 2).Insert Shift:=xlDown
Application.CutCopyMode = False
Set r = Range(Cells(2, 1), Cells(lRow + n - 1, 1))
Lnks = Cells(2, "N").Value
For i = 0 To n - 1
j = InStr(i + 5, Lnks, "-", vbTextCompare)
m = Application.Match(Mid(Lnks, j - 4, 6), r, 0) + 2
Rows("2:2").Cut
Rows(m & ":" & m).Insert Shift:=xlDown
If m > r.Rows.Count Then
Set r = Range(Cells(2, 1), Cells(Cells(Rows.Count, 1).End(xlUp).Row, 1))
End If
Next i
End If
Wend
End Sub