Sorting excel sheets in numerical order based on the date in the batch number

Daisy0909

New Member
Joined
Oct 12, 2021
Messages
4
Office Version
  1. 2016
Platform
  1. Windows
Hello.

This is something I am struggling with. I have a large number of sheets in an excel spreadsheet that aren’t in date order. They are titled with batch numbers I.e “AB 100921A” or “AB 010616A” etc.

Is there a way to automatically put the sheets in order by the date within the batch number?

I’ve seen many ways to do this if it’s just a date of a word but I’m finding it very difficult to do this with batch numbers.
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Hi @Daisy0909. Welcome to the Forum
In your system "100921" means October 9 or September 10?
 
Upvote 0
Try it on a copy of your workbook:
VBA Code:
Sub SortTabsByDate()

Application.ScreenUpdating = False
Dim ShCount As Long, i As Long, j As Long
Dim n1 As Long, n2 As Long, s1 As String, s2 As String

ShCount = Sheets.Count

For i = 1 To ShCount - 1
    For j = i + 1 To ShCount

        s1 = Sheets(i).Name
        s2 = Sheets(j).Name
        n1 = DateSerial(Mid(s1, 8, 2), Mid(s1, 6, 2), Mid(s1, 4, 2))
        n2 = DateSerial(Mid(s2, 8, 2), Mid(s2, 6, 2), Mid(s2, 4, 2))

        If n2 < n1 Then
            Sheets(j).Move Before:=Sheets(i)
        End If

    Next j
Next i

Application.ScreenUpdating = True

End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,223,214
Messages
6,170,772
Members
452,353
Latest member
strainu

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