Sub test()
Dim oneCol as Range
For Each oneCol in Range("A1:D45").Columns
Call FormatSort(oneCol)
Next oneCol
End Sub
Sub FormatSort(sourceRange As Range, Optional destRange As Range)
Dim arrData As Variant
Dim i As Long
If destRange Is Nothing Then Set destRange = sourceRange
arrData = sourceRange.Value
For i = 1 To sourceRange.Rows.Count
If Len(arrData(i, 1)) <> 0 Then
arrData(i, 1) = UBIprefix(sourceRange.Cells(i, 1)) & arrData(i, 1)
End If
Next i
sourceRange.Copy Destination:=destRange.Resize(sourceRange.Rows.Count, 1)
With destRange.Resize(sourceRange.Rows.Count, 1)
sourceRange.Copy Destination:=.Cells
.Value = arrData
.Sort key1:=.Cells(1, 1), order1:=xlAscending
arrData = .Value
For i = 1 To .Rows.Count
arrData(i, 1) = Mid(arrData(i, 1), 4)
Next i
.Value = arrData
End With
End Sub
Function UBIprefix(ByRef aRange As Range)
Const chrNot = "Z"
Set aRange = aRange.Cells(1, 1)
With aRange
If UCase(.Text) = .Text Then
UBIprefix = "A"
ElseIf Application.Proper(.Text) = .Text Then
UBIprefix = "B"
Else
UBIprefix = chrNot
End If
If .Font.Bold Then
UBIprefix = UBIprefix & "B"
Else
UBIprefix = UBIprefix & chrNot
End If
If .Font.Italic Then
UBIprefix = UBIprefix & "i"
Else
UBIprefix = UBIprefix & chrNot
End If
End With
End Function