Consulting

Results 1 to 6 of 6

Thread: Worksheets numerically sorting

  1. #1

    Red face Worksheets numerically sorting

    Dear VBAExpress friends,
    this is my very first post on this forum... I really admire Your knowledge and enthusiasm here... You're all great!!

    I have a question that might be an easy-peasy for You...

    I'm dealing with worksheets in excel that are named by numbers (eg. 2.9, 2.10, 2.93) and I want to sort them numerically.
    But I'm a bit confused by the decimal places that the names have, and I'm not sure how to write the right code.

    When I sort them (with the macro that I'm using), names like, for example, 2.9 appears after 2.85 (which is kinda right),
    but my 2.9 should be before it..... You can take a look on the photos attached.....You'll get it there Thank You!


    At the moment I'm using this code:

    Sub WSsort()
    Application.ScreenUpdating = False
    Dim ShCount As Integer, i As Integer, j As Integer
    ShCount = Sheets.Count
    For i = 1 To ShCount - 1
        For j = i + 1 To ShCount
            If UCase(Sheets(j).Name) < UCase(Sheets(i).Name) Then
                Sheets(j).Move before:=Sheets(i)
            End If
        Next j
    Next i
    Application.ScreenUpdating = True
    End Sub
    Random.jpgTocno.jpgKAOPoslozeno.jpg
    Last edited by Aussiebear; 11-26-2021 at 06:14 AM. Reason: Added code tags to supplied code

  2. #2
    I found out that KUTOOLS does the thing that I need...
    He manage to sort the worksheets in a Alpha-Numeric order... (take a look on the photo attached).

    But, is there a way to see the code that runs behind this action? So I can integrate it in my own code?

    Screenshot_1.jpg

    Thank You!

  3. #3
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,054
    Location
    Not unless the programmer behind Kutools wants to share it with you.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  4. #4
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,873
    Try blah (which call the function below it):
    Sub blah()
    ReDim ShtNames(1 To Sheets.Count)
    'get the sheet names:
    For i = 1 To Worksheets.Count
      ShtNames(i) = Worksheets(i).Name
    Next i
    'sort the sheet names:
    For j = 1 To UBound(ShtNames) - 1
      For i = 1 To UBound(ShtNames) - 1
        If Not aLessThanb(ShtNames(i), ShtNames(i + 1)) Then
          temp = ShtNames(i)
          ShtNames(i) = ShtNames(i + 1)
          ShtNames(i + 1) = temp
        End If
      Next i
    Next j
    'Move the sheets:
    For Each nm In ShtNames
      Sheets(nm).Move after:=Sheets(Sheets.Count)
    Next nm
    End Sub
    
    
    Function aLessThanb(a, b) As Boolean
    x = Split(CStr(a), ".")
    y = Split(CStr(b), ".")
    For i = 0 To Application.Min(UBound(x), UBound(y))
      Z = Application.Max(Len(x(i)), Len(y(i)))
      x(i) = Format(x(i), String(Z, "0"))
      y(i) = Format(y(i), String(Z, "0"))
    Next i
    aLessThanb = Join(x) < Join(y)
    End Function
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  5. #5
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    1. Do your ws names really have a decimal point at the end also, e.g. 2.10<point> because that's the way in looks in your screen shots??

    2. Why do you want 2.7. and 2.9. to go before 2.10. ? If they were 2.07 and 2.09 I could see it
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  6. #6
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,873
    As I see it, the OP has the full stops/periods/decimal points as delimiters rather than as decimal points, so whatever is between them is a whole number.
    This is like the numbering of Bills of Material, or paragraph numbering in legal documents.
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

Posting Permissions

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