Select the range to sort before run.
Option Explicit
Private RegX As Object
Sub test()
Dim a, i As Long, ii As Long, x, t As Long
Set RegX = CreateObject("VBScript.RegExp")
With Selection
a = .Value
With CreateObject("System.Collections.SortedList")
For ii = 1 To UBound(a, 2)
For i = 1 To UBound(a, 1)
If a(i, ii) <> "" Then
x = GetSortVal(a(i, ii))
If Not .Contains(x) Then
.Item(x) = Array(1, a(i, ii))
Else
.Item(x) = Array(.Item(x)(0) + 1, .Item(x)(1))
End If
End If
Next
Next
ReDim a(1 To UBound(a, 1), 1 To UBound(a, 2)): x = 1
For i = 0 To .Count - 1
For ii = 1 To .GetByIndex(i)(0)
t = t + 1
If t > UBound(a, 2) Then x = x + 1: t = 1
a(x, t) = .GetByIndex(i)(1)
Next
Next
End With
.Value = a
End With
Set RegX = Nothing
End Sub
Function GetSortVal(ByVal txt As String) As String
Dim m As Object, i As Long
With RegX
.Global = True
.Pattern = "\d+"
If .test(txt) Then
For i = .Execute(txt).Count - 1 To 0 Step -1
Set m = .Execute(txt)(i)
txt = Application.Replace(txt, m.firstindex + 1, _
m.Length, Format$(m.Value, String(12, "0")))
Next
End If
End With
GetSortVal = txt
End Function
Bookmarks