Consulting

Results 1 to 3 of 3

Thread: Sort Sheets in ascending order

  1. #1
    VBAX Regular
    Joined
    Feb 2009
    Posts
    54
    Location

    Sort Sheets in ascending order

    Hi,

    I am trying to sort sheets in a workbook in a numerical order.

    Suppose I have 10 sheets namely ; 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

    Now when I try to sort them using VBA the result is :-

    1, 10, 2, 3, 4, 5, 6, 7, 8

    ??

    Here's the code i'm using:

    Sub SortWorksheets()
    Dim sCount As Integer, i As Integer, j As Integer
        Application.ScreenUpdating = False
        sCount = Worksheets.Count
        If sCount = 1 Then Exit Sub
        For i = 1 To sCount - 1
            For j = i + 1 To sCount
                If Worksheets(j).Name < Worksheets(i).Name Then
                    Worksheets(j).Move Before:=Worksheets(i)
                End If
            Next j
        Next i
    End Sub

    Any solutions??

    Tried the code from the KB as well.. But getting the same problem..

    Thanks
    Last edited by Aussiebear; 04-25-2023 at 09:49 PM. Reason: Adjusted the code tags

  2. #2
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,198
    Location
    Try this...

    Sub SortWorksheets()
    Dim sCount As Integer, i As Integer, j As Integer
    Application.ScreenUpdating = False
    sCount = Worksheets.Count
    If sCount = 1 Then Exit Sub
    For i = 1 To sCount - 1
        For j = i + 1 To sCount
            If Int(Worksheets(j).Name) < Int(Worksheets(i).Name) Then
                Worksheets(j).Move Before:=Worksheets(i)
            End If
       Next j
    Next i
    Application.ScreenUpdating = True
    End Sub

    Hope this helps
    Last edited by Aussiebear; 04-25-2023 at 09:50 PM. Reason: Adjusted the code tags
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved
    Click here for a guide on how to upload a file with your post

    Excel 365, Version 2403, Build 17425.20146

  3. #3
    VBAX Regular
    Joined
    Feb 2009
    Posts
    54
    Location
    If Int(Worksheets(j).Name) < Int(Worksheets(i).Name) Then
    Perfect!!

    Thanks a ton..

    Regards
    Last edited by Aussiebear; 04-25-2023 at 09:50 PM. Reason: Adjusted the code tags

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •