1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| Sub test()
Debug.Print regroup("1, 2, 3, 5, 6, 9, 11, 12")
End Sub
Function regroup(s As String) As String
Dim pl As Range, i As Long, tmp, tmp2
If s = "" Then regroup = "": Exit Function
tmp = Split(Application.Trim(s), ",")
For i = 0 To UBound(tmp)
If pl Is Nothing Then Set pl = Rows(tmp(i)) Else Set pl = Union(pl, Rows(tmp(i)))
Next i
tmp = Split(pl.Address(False, False), ",")
For i = 0 To UBound(tmp)
tmp2 = Split(tmp(i), ":")
If tmp2(0) = tmp2(1) Then tmp(i) = tmp2(0) Else tmp(i) = Join(tmp2, "->")
Next i
regroup = Join(tmp, ", ")
End Function |
Partager