+ Reply to Thread
Results 1 to 5 of 5

Sorting in Wrapped Grid

Hybrid View

  1. #1
    Registered User
    Join Date
    11-27-2018
    Location
    Philadelphia, PA
    MS-Off Ver
    2013
    Posts
    2

    Sorting in Wrapped Grid

    Hello,

    I am wondering if it's possible to sort values Left-Right AND Top-Bottom so that you wind up with a sorted grid. I have a large spreadsheet where all values need to be simultaneously in view, so we can't keep all values in a row or column, but I have not found a way to sort so that they remain in order in this grid form. I've taken a screen shot of an example of the type of sort function i'd like to preform: Unsorted grid values on the left, sorted on the right.

    Sort(X&Y).jpg

    Thank you for your help!

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Sorting in Wrapped Grid

    Via formula,

    A
    B
    C
    D
    E
    F
    G
    H
    I
    1
    2
    1
    6
    5
    1
    2
    3
    E2: =SMALL($A$2:$C$4, 3 * ROWS($A$2:A2) + COLUMNS($A$2:A2) - 3)
    3
    7
    2
    8
    4
    5
    6
    4
    3
    9
    4
    7
    8
    9


    That might not scale well for a large grid.
    Last edited by shg; 11-27-2018 at 06:50 PM.
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Registered User
    Join Date
    11-27-2018
    Location
    Philadelphia, PA
    MS-Off Ver
    2013
    Posts
    2

    Re: Sorting in Wrapped Grid

    Hey shg,

    That's a nifty solution! I should have mentioned- the values i'm trying to sort are Addresses- they start with numbers but contain letters, so they can't be sorted using the SMALL function, I don't think.
    And even though in my example I showed a sorted grid next to an unsorted one, Ideally I'd like to just sort the existing cells without creating a fraternal twin.

    Is this just not possible with excel?

    Thanks,
    Shane

  4. #4
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Sorting in Wrapped Grid

    This sub will sort a grid.
    Sub WrapSort(aRange As Range, Optional Descending As Boolean = False, Optional WrapVertical As Boolean = False)
        Dim arrImage As Variant, arrValue As Variant
        Dim oneValue As Variant
       
        Dim i As Long, j As Long, k As Long
        
        arrImage = aRange.Value
        
        Rem create 1-D array of values
        ReDim arrValue(1 To aRange.Count)
        i = 0
        For Each oneValue In arrImage
            If IsError(oneValue) Then oneValue = CStr(oneValue)
            i = i + 1
            arrValue(i) = oneValue
        Next oneValue
        
        Rem sort arrValue
        For i = 1 To UBound(arrValue) - 1
            For j = i + 1 To UBound(arrValue)
                If (arrValue(j) < arrValue(i)) Xor Descending Then
                    oneValue = arrValue(i)
                    arrValue(i) = arrValue(j)
                    arrValue(j) = oneValue
                End If
            Next j
        Next i
        
        Rem put sorted values in 2-D array (in order)
        
        i = 1: j = 1
        
        For k = 1 To UBound(arrValue)
            arrImage(i, j) = arrValue(k)
            
            If WrapVertical Then
                i = i + 1
                If aRange.Rows.Count < i Then
                    i = 1
                    j = j + 1
                End If
            Else
                j = j + 1
                If aRange.Columns.Count < j Then
                    j = 1
                    i = i + 1
                End If
            End If
        Next k
        
        aRange.Value = arrImage
    End Sub

    Note the two optional arguments Descending and WrapVertical.
    WrapVertical is the diference betweeen
    1 2 3
    4 5 6
    7 8 9
    and
    1 4 7
    2 5 8
    3 6 9
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

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

    Re: Sorting in Wrapped Grid

    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

+ 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. VBA Grid x Grid SUM problems
    By stedaley in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 11-27-2015, 10:08 AM
  2. Replies: 1
    Last Post: 10-29-2013, 02:11 AM
  3. Replies: 2
    Last Post: 12-15-2011, 04:14 AM
  4. wrapped text?
    By tregrad in forum Excel General
    Replies: 4
    Last Post: 12-12-2009, 04:23 PM
  5. Limit to Wrapped Text?
    By Llobid in forum Excel General
    Replies: 5
    Last Post: 03-07-2006, 12:10 AM
  6. [SOLVED] I need to convert grid.doc to an Excel Grid.
    By suavejohn in forum Excel Charting & Pivots
    Replies: 0
    Last Post: 09-01-2005, 02:05 PM
  7. [SOLVED] grid & grid refrences
    By john redmanshaw in forum Excel - New Users/Basics
    Replies: 1
    Last Post: 02-06-2005, 03:06 PM

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