A couple years ago, by browsing different forums I made myself a macro that was sorting columns by length, longest to shortest (by number of characters in cells). I was pasting special with transpose to a new sheet to get rows listed as columns. I then pasted the VBS code in the macro 100 times so it could do 100 columns per run.
Today I tried running this macro but it doesn't work at all now :(
This is the VBS code I used (without the 100 pastes):
Sub SortByLength2()
Dim lLoop As Long
Dim lLoop2 As Long
Dim str1 As String
Dim str2 As String
Dim MyArray
Dim lLastRow As Long
lLastRow = Range("A65536").End(xlUp).Row
MyArray = Range(Cells(2, 1), Cells(lLastRow, 1))
'Sort array
For lLoop = 1 To UBound(MyArray)
For lLoop2 = lLoop To UBound(MyArray)
If Len(MyArray(lLoop2, 1)) > Len(MyArray(lLoop, 1)) Then
str1 = MyArray(lLoop, 1)
str2 = MyArray(lLoop2, 1)
MyArray(lLoop, 1) = str2
MyArray(lLoop2, 1) = str1
End If
Next lLoop2
Next lLoop
'Output sorted array
Range("JO1:JO" & UBound(MyArray) + 1) = (MyArray)
Range("A:A").Delete Shift:=xlToLeft
End Sub
There should be a better solution to sort in rows, without transposing rows to columns and without pasting the same VBS code 100 times...
Could anyone help me with the macro that could simply sort cells in rows by length of characters in each cell with unlimited rows and columns? Longest cells should be 1st, shortest - last
In my case, I have 745 rows and column range from A to BA.
Thanks in advance
Update, as per request, a screnshot: