+ Reply to Thread
Results 1 to 13 of 13

A difficult sort

Hybrid View

  1. #1
    Registered User
    Join Date
    12-20-2008
    Location
    Arkansas
    MS-Off Ver
    2010
    Posts
    87

    A difficult sort

    I need to sort a list, rename sheet tabs, put sheets in sorted order, according to a defined sort list.
    I have attached a spreadsheet with sample lists and descriptions of what is needed.
    Thanks,
    Doug
    Attached Files Attached Files
    Last edited by Obfuscated; 01-12-2013 at 06:33 PM.

  2. #2
    Forum Contributor Toonies's Avatar
    Join Date
    07-30-2009
    Location
    Newcastle, UK
    MS-Off Ver
    Excel 2016
    Posts
    511

    Re: A difficult sort

    is this what you need

    Sorting1.xlsm

  3. #3
    Valued Forum Contributor
    Join Date
    07-29-2009
    Location
    Belgium
    MS-Off Ver
    Excel 2003/Excel 2010
    Posts
    534

    Re: A difficult sort

    For sorting the list, see attachment.
    Attached Files Attached Files
    Last edited by WHER; 01-01-2013 at 08:10 AM.

  4. #4
    Registered User
    Join Date
    12-20-2008
    Location
    Arkansas
    MS-Off Ver
    2010
    Posts
    87

    Re: A difficult sort

    Thanks to both of you, you've given me many good tips. WHER seems to be closest to what I need though, at this point. To further my problem... You have "Hard-coded" the column "A" listing and the listing in column "C". These change daily and might be W12X336, W14X99, etc... the list in column "C" is weights available while the "W40" is a width size. If these change from day to day, how could I use your code to be more "generic" in that regard? Also the list in column "A" could be from 4 to 30 lines. Column "B" will alsways be static. Column "C" could be from 1 to 20 lines.
    I'm going to start working with what I have from now and see what I can do with it.

    Thanks again,

    Doug

  5. #5
    Valued Forum Contributor
    Join Date
    07-29-2009
    Location
    Belgium
    MS-Off Ver
    Excel 2003/Excel 2010
    Posts
    534

    Re: A difficult sort

    Attachment in post 3 replaced, to reflect your comments above.

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

    Re: A difficult sort

    Try the attached.

    This code is capable to handle the records that doesn't match to the list.
    (list those to the last)
    Option Explicit
    
    Sub test()
        mySort
        AddSheet
    End Sub
    
    Private Sub mySort()
        Dim a, i As Long, ii As Long, SL As Object
        Dim myNum As String, Grade, w
        Set SL = CreateObject("System.Collections.SortedList")
        ReDim w(1 To 2)
        With Sheets("Lists").Range("a1").CurrentRegion
            a = .Value
            For i = 2 To UBound(a, 1)
                If a(i, 3) <> "" Then
                    a(i, 3) = CStr(a(i, 3))
                    If Not SL.contains(i) Then
                        w(1) = a(i, 3)
                        Set w(2) = CreateObject("System.Collections.ArrayList")
                        SL(i) = w
                    End If
                    w = SL(i)
                    For ii = 2 To UBound(a, 1)
                        If a(ii, 2) <> "" Then
                            a(ii, 2) = Replace(a(ii, 2), "-", "")
                            w(2).Add a(ii, 2)
                            SL(i) = w
                        Else
                            Exit For
                        End If
                    Next
                Else
                    Exit For
                End If
            Next
            ReDim Preserve a(1 To UBound(a, 1), 1 To 1)
            ReDim Preserve a(1 To UBound(a, 1), 1 To 2)
            For i = 2 To UBound(a, 1)
                myNum = "zzz": Grade = "zzz"
                If a(i, 1) Like "*X*" Then
                    myNum = Val(Mid$(a(i, 1), InStr(1, a(i, 1), "X", 1) + 1))
                End If
                If a(i, 1) Like "* *" Then
                    Grade = Replace(Split(a(i, 1) & "-")(1), "-", "")
                End If
                For ii = 0 To SL.Count - 1
                    If SL.GetByIndex(ii)(1) = myNum Then
                        a(i, 2) = Format$(ii, "00000") & _
                        Format$(SL.GetByIndex(ii)(2).IndexOf(Grade, 0), "00000")
                        Exit For
                    End If
                Next
                If a(i, 2) = "" Then a(i, 2) = "zzz" & a(i, 1)
            Next
            SL.Clear
            For i = 2 To UBound(a, 1)
                SL(a(i, 2)) = a(i, 1)
            Next
            For i = 0 To SL.Count - 1
                a(i + 2, 1) = SL.GetByIndex(i)
            Next
            .Resize(, 1).Value = a
        End With
    End Sub
    
    Private Sub AddSheet()
        Dim i As Long
        With Sheets("Lists")
            For i = .Range("a" & Rows.Count).End(xlUp).Row To 2 Step -1
                If Not IsSheetExists(.Cells(i, 1).Value) Then
                    Sheets.Add(after:=Sheets("Lists")).Name = .Cells(i, 1).Value
                End If
                With Sheets(.Cells(i, 1).Value)
                    .Move after:=Sheets("Lists")
                    .Cells(1).Value = .Name
                End With
            Next
        End With
    End Sub
    
    Function IsSheetExists(ByVal txt As String) As Boolean
        On Error Resume Next
        IsSheetExists = Len(Sheets(txt).Name)
        On Error GoTo 0
    End Function
    Attached Files Attached Files

  7. #7
    Registered User
    Join Date
    12-20-2008
    Location
    Arkansas
    MS-Off Ver
    2010
    Posts
    87

    Re: A difficult sort - SOLVED

    JINDON !!! Absolutely GREAT. Thanks. Not being a trained programmer, I stumble with quite a few of these tasks. But with the great help I get here, you guys/gals make a great difference. Thanks again, I'm just waiting for the chance to get to help someone else (maybe one of these days...).


    HMMM..Can't find the "SOLVED" option.

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

    Re: A difficult sort

    You are welcome.

  9. #9
    Registered User
    Join Date
    12-20-2008
    Location
    Arkansas
    MS-Off Ver
    2010
    Posts
    87

    Re: A difficult sort

    jindon,

    I have run into a problem, and since this was your code, I figured you would be the best help. (Any help from anyone else is always appreciated.)

    I have attached another workbook with a simple list and the trouble described.

    Thanks,
    Doug
    Attached Files Attached Files

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

    Re: A difficult sort

    I din't understand your intention of changing the code as such....

    It is obvious to get the error since you haven't got all the sheets listed.

  11. #11
    Registered User
    Join Date
    12-20-2008
    Location
    Arkansas
    MS-Off Ver
    2010
    Posts
    87

    Re: A difficult sort

    Ok, I was trying to conserve space by sending an abbreviated workbook. Attached is the workbook I'm developing. It has sheets that have been loaded and is now at the point of sorting the sheets and putting them in the sorted order.
    When you open the workbook, you will be put into a select mode, just click "cancel" and then you can see what I'm running into.

    I was running the sort routine manually until I get everything worked out.

    The "Lists" is the original. I used the "Lists1" sheet because the info on the "Lists" sheet is not located the same as the other.

    Thanks,

    Doug
    Attached Files Attached Files

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

    Re: A difficult sort

    OK, since your data layouts are slightly different, try change to
            SL.Clear
            For i = 2 To UBound(a, 1)
                If a(i,1) <> "" Then SL(a(i, 2)) = a(i, 1)
            Next

  13. #13
    Registered User
    Join Date
    12-20-2008
    Location
    Arkansas
    MS-Off Ver
    2010
    Posts
    87

    Re: A difficult sort

    Once again, many thanks. That works great!

    Doug

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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