How can I sort worksheets in ascending/descending order based on the value of the same cell in each sheet?

Davao_Danny

Board Regular
Joined
Aug 31, 2009
Messages
56
Hi everyone.

I need two similar macros: one each for sorting worksheets in ascending/descending order based on the value of the same cell in each worksheet.

The first and last worksheets (named as "cover page" and "summary") need to remain where they are, but every worksheet in between these needs to be sorted based on the value of cell A5 in each.

Any help will be highly appreciated.

Thanks in advance.
Danny :confused:<!-- / message -->
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
It could probably be a whole lot easier if the value in A5 in each sheet is guaranteed to be numeric. This method however can also sort based on textual values in A5, because it uses the normal sort method.

Code:
Sub CustomSortSheets()
'// sorts sheets in an order according to values housed in a A5 in each sheet
    Dim arrVALUES()
    Dim lngSHEETSCOUNT As Long
    Dim wks As Worksheet
    Dim rngCELL As Range
    
    lngSHEETSCOUNT = ActiveWorkbook.Sheets.Count
    ReDim arrVALUES(1 To lngSHEETSCOUNT, 1 To 2)
    
    For Each wks In Worksheets
        arrVALUES(wks.Index, 1) = wks.Range("A5").Value
        arrVALUES(wks.Index, 2) = wks.Name
    Next wks
    
    With Sheets.Add
        With .Range("A1:B" & lngSHEETSCOUNT)
            .Value = arrVALUES
            .Sort key1:=.Cells(1)
        End With
        On Error Resume Next
            For Each rngCELL In .Range("B1:B" & lngSHEETSCOUNT)
                Sheets(rngCELL.Text).Move after:=Sheets(rngCELL.Offset(-1).Text)
            Next rngCELL
        On Error GoTo 0
        Application.DisplayAlerts = False
            .Delete
        Application.DisplayAlerts = True
    End With
End Sub
 
Upvote 0
Jon,

The code you posted works perfectly. Is it possible to also have the code ignore specific sheets by adding in an if statement like this:

Code:
For Each wks In Worksheets
    
       If  wks.Name <> "Overview" And wks.Name <> "List" And wks.Name  <> "Template" And wks.Name <> "First" Then
Else
     
        arrVALUES(wks.Index, 1) = wks.Range("E13").Value
        arrVALUES(wks.Index, 2) = wks.Name
        End If
     Next wks

I tried this, but the code no longer works.
 
Upvote 0

Forum statistics

Threads
1,216,036
Messages
6,128,432
Members
449,452
Latest member
Chris87

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