VBA - Sort Worksheets in same order as list

imcfcl

New Member
Joined
Sep 23, 2011
Messages
18
Morning Guys,

I should probably credit you guys for most of the clever stuff behind a project I'm working on...

I have the below code that sorts a list of Doors that I have in row C17 downwards. Door 54, Door 7, Door 109 etc. The list is feeding a drop down box, so I needed this to help people find the door they were looking for, in the said drop down box.

Code:
Sub ListSorter()
    Dim LastRow As Long
    LastRow = Range("C" & Rows.Count).End(xlUp).row
    Range("F17").Copy Range("D17")
    Application.CutCopyMode = False
    Range("D17").AutoFill Destination:=Range("D17:D" & LastRow)
    ActiveWorkbook.Worksheets("Home").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Home").Sort.SortFields.Add Key:=Range("D17:D" & LastRow) _
        , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortTextAsNumbers
    With ActiveWorkbook.Worksheets("Home").Sort
        .SetRange Range("C17:D" & LastRow)
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
       Range("D17:D" & LastRow).Select
       Selection.ClearContents
End Sub

I wondered if there was some code I could add to the end of this, that would the sort worksheets, which are all named after each cell in the list, in the same order.
 
Try this in a copy of your workbook. Add the blue bits to your existing code.

Rich (BB code):
Sub ListSorter()
    Dim LastRow As Long
    Dim c As Range
    

    'The rest of your existing code here


    Application.ScreenUpdating = False
    On Error Resume Next
    For Each c In Worksheets("Home").Range("C17:C" & LastRow)
        Sheets(c.Value).Move After:=Sheets(Sheets.Count)
    Next c
    On Error GoTo 0
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
So happy with the result, It worked with two exceptions, those being sheets called Door 123, and Door 77A, but I'll add a bit of code to move them after Door 122 and Door 77

Thank you so much - again
 
Upvote 0
It worked with two exceptions, those being sheets called Door 123, and Door 77A,
Was that my code not putting them in the order they appeared in C17:C & LastRow or your code not sorting them in the way you wanted?
 
Upvote 0

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top