Sort Sheet Tabs Named in Date (D.M.YYYY)

ayman_salah

New Member
Joined
Aug 11, 2022
Messages
3
Office Version
  1. 2021
Platform
  1. Windows
I have a workbook and the first sheet on it is named "Home" and after that I have some sheets with the names of dates( for example 21.7.2022, 1.8.2022, .......etc) and I want automatically the excel to sort them descending (1.8.2022, 21.7.2022, ....). except the first sheet throw autorun macro. Thank you very much.
 

Attachments

  • 2022-08-11.png
    2022-08-11.png
    200.9 KB · Views: 4

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Welcome to the MrExcel board!

See if this does what you want. Test with a copy of your workbook.

VBA Code:
Sub SortSheets()
  Dim AL As Object
  Dim i As Long
  Dim DateBits As Variant
  
  Application.ScreenUpdating = False
  Set AL = CreateObject("System.Collections.ArrayList")
  For i = 2 To Sheets.Count
    DateBits = Split(Sheets(i).Name, ".")
    AL.Add DateBits(2) & Format(DateBits(1), "00") & Format(DateBits(0), "00") & Sheets(i).Name
  Next i
  AL.Sort
  For i = AL.Count - 1 To 0 Step -1
    Sheets(Mid(AL.Item(i), 9)).Move After:=Sheets(Sheets.Count)
  Next i
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
Welcome to the MrExcel board!

See if this does what you want. Test with a copy of your workbook.

VBA Code:
Sub SortSheets()
  Dim AL As Object
  Dim i As Long
  Dim DateBits As Variant
 
  Application.ScreenUpdating = False
  Set AL = CreateObject("System.Collections.ArrayList")
  For i = 2 To Sheets.Count
    DateBits = Split(Sheets(i).Name, ".")
    AL.Add DateBits(2) & Format(DateBits(1), "00") & Format(DateBits(0), "00") & Sheets(i).Name
  Next i
  AL.Sort
  For i = AL.Count - 1 To 0 Step -1
    Sheets(Mid(AL.Item(i), 9)).Move After:=Sheets(Sheets.Count)
  Next i
  Application.ScreenUpdating = True
End Sub

The code is working fine thanks, Please can you explain step by step how the Code is working
 
Upvote 0
The code puts all the sheet names into an "ArrayList" in the format of "yyyymmdd" followed by the actual sheet name. So 17.5.2022 goes in as "2022051717.5.2022"
5.6.2022 goes in as "202206055.6.2022" etc
The list is then sorted which puts them all in date order due to the structure of the blue parts.
Then work through the sorted list from back to front and cut off the first 8 characters (blue) which returns back to the original name (red) to place each one at the end leaving them in the order you want
 
Upvote 0
Welcome to the MrExcel Message Board!

Cross-posting (posting the same question in more than one forum) is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.

Be sure to follow & read the link at the end of the rule too!

Cross posted at:

If you have posted the question at more places, please provide links to those as well.

If you do cross-post in the future and also provide links, then there shouldn’t be a problem.
 
Upvote 0

Forum statistics

Threads
1,215,720
Messages
6,126,443
Members
449,314
Latest member
MrSabo83

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