+ Reply to Thread
Results 1 to 2 of 2

sorting by character length

Hybrid View

  1. #1
    Registered User
    Join Date
    09-23-2008
    Location
    chicago
    Posts
    27

    Exclamation sorting by character length

    I have a list of a 3000 items, I need to sort them by character length...

    I cannot figure out how to do this in the normal auto sort functions...

    Digging online I found some code like this that seems like it will do what I want:

    Sub SortByLength()
       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("A2:A" & UBound(MyArray) + 1) = (MyArray)
    End Sub
    But I cannot figure out how to get this to run... I have been reading about Macros, but cannot get this to work...

    please help

  2. #2
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: sorting by character length

    You could do this without a macro by using a helper column.

    In the spare column (helper column), say B2
    =LEN(A2)
    Drag/Fill Down to end of list

    Then sort on Column B

    Hope this helps

    [EDIT]
    If you really need a macro just automate the above.
    Sub SortByLength()
       Dim LastRow As Long
    
       LastRow = Range("A" & Rows.Count).End(xlUp).Row
       
       Range("B2:B" & LastRow).Formula = "=LEN(A2)"
       
       Range("A2:B" & LastRow).Sort Key1:=Range("B2:B23"), Order1:=xlAscending, Header:=xlYes
       
       Range("B2:B" & LastRow).Clear
       
    End Sub
    Just change the references to ColumnB to a suitable spare column.
    Attached Files Attached Files
    Last edited by Marcol; 10-03-2010 at 12:39 PM. Reason: Macro added
    If you need any more information, please feel free to ask.

    However,If this takes care of your needs, please select Thread Tools from menu above and set this topic to SOLVED. It helps everybody! ....

    Also
    اس کی مدد کرتا ہے اگر
    شکریہ کہنے کے لئے سٹار کلک کریں
    If you are satisfied by any members response to your problem please consider using the small Star icon bottom left of their post to show your appreciation.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1