+ Reply to Thread
Results 1 to 5 of 5

Sort by Length on Range A52:A54

Hybrid View

  1. #1
    Valued Forum Contributor meabrams's Avatar
    Join Date
    10-03-2014
    Location
    USA
    MS-Off Ver
    2007, 2010, 2013, 2016
    Posts
    451

    Sort by Length on Range A52:A54

    I need to either adjust this macro so that it will work with the range A52:A54 sorting them by length putting the longest text at the top.

    The one I found after I change it around a bit works if starting at A1... if starting any where else it changes everything above to an #N/A

    Thank you for your help

    Macro I found and currently trying to adjust
    Sub SortByLength()
        Dim lLoop As Long
        Dim lLoop2 As Long
        Dim str1 As String
        Dim str2 As String
        Dim MyArray
        Dim lLastRow As Long
         
    
        MyArray = Range("A52:A54") 'works if set to Range("A1:A3") 
         '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("A52:A" & UBound(MyArray)) = (MyArray)
    End Sub
    
    original macro post here http://www.ozgrid.com/forum/showthread.php?t=80743

  2. #2
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Sort by Length on Range A52:A54

    Hi meabrams,

    Try the following (changes in red):
    Sub SortByLength()
        Dim lLoop As Long
        Dim lLoop2 As Long
        Dim str1 As String
        Dim str2 As String
        Dim MyArray As Variant
        Dim lLastRow As Long
         
    
        MyArray = Range("A52:A54") 'works if set to Range("A1:A3")
         '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("A52:A" & (51 + UBound(MyArray))) = (MyArray)
    End Sub
    Lewis

  3. #3
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,835

    Re: Sort by Length on Range A52:A54

    meabrams,

    Isn't it just

    Sub test()
        Dim a, i As Long, ii As Long, temp
        With [a52:a54]
            a = .Value
            For i = 1 To UBound(a, 1) - 1
                For ii = i + 1 To UBound(a, 1)
                    If Len(a(i, 1)) < Len(a(ii, 1)) Then
                        temp = a(i, 1)
                        a(i, 1) = a(ii, 1)
                        a(ii, 1) = temp
                    End If
                Next
            Next
            .Value = a
        End With
    End Sub

  4. #4
    Valued Forum Contributor meabrams's Avatar
    Join Date
    10-03-2014
    Location
    USA
    MS-Off Ver
    2007, 2010, 2013, 2016
    Posts
    451

    Re: Sort by Length on Range A52:A54

    I will try these both after I get up in the afternoon...

  5. #5
    Valued Forum Contributor meabrams's Avatar
    Join Date
    10-03-2014
    Location
    USA
    MS-Off Ver
    2007, 2010, 2013, 2016
    Posts
    451

    Re: Sort by Length on Range A52:A54

    Thank you both I ended up going with LJMetzger way, but I will keep jindon way for next time it does look cleaner though

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Trying to sort using a variable for length of spreadsheet
    By Fyredancer in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-23-2013, 02:09 PM
  2. Sort Column by Length
    By vargs in forum Excel General
    Replies: 2
    Last Post: 06-24-2010, 04:32 PM
  3. Sort Ascending for variable Report length
    By Hblbs in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-11-2009, 08:33 AM
  4. Sort by text length
    By holyearth in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-18-2007, 09:50 AM
  5. Sort Columns by Cell Character length?
    By paulz in forum Excel General
    Replies: 1
    Last Post: 10-16-2006, 02:33 PM
  6. Sort text list by character length - how?
    By masterdiablo13 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 07-16-2006, 02:45 PM
  7. How do I sort a large column of data by length?
    By blange in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 05-29-2006, 03:40 PM

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