Hi All
I have a workbook with multiple sheets .
Looking for fast vba code to sort sheet names alphabetically.
Also note some sheets are alpha numeric that is 01_15_ Sodium Peroxide Analysis
Thanks
Hi All
I have a workbook with multiple sheets .
Looking for fast vba code to sort sheet names alphabetically.
Also note some sheets are alpha numeric that is 01_15_ Sodium Peroxide Analysis
Thanks
Try this code
Sub SortSheets() Dim I As Integer, J As Integer For I = 1 To Sheets.Count - 1 For J = I + 1 To Sheets.Count If Val(Sheets(I).Name) > Val(Sheets(J).Name) Then Sheets(J).Move Before:=Sheets(I) End If Next J Next I End Sub
< ----- Please click the little star * next to add reputation if my post helps you
Visit Forum : From Here
Hi JEAN1972,
Try this Code I saw on Mr. Excel some time ago.)
Regards.Sub Sort_Active_Book() Dim i As Integer Dim j As Integer Dim iAnswer As VbMsgBoxResult ' ' Prompt the user as which direction they wish to ' sort the worksheets. ' iAnswer = MsgBox("Sort Sheets in Ascending Order?" & Chr(10) _ & "Clicking No will sort in Descending Order", _ vbYesNoCancel + vbQuestion + vbDefaultButton1, "Sort Worksheets") For i = 1 To Sheets.Count For j = 1 To Sheets.Count - 1 ' ' If the answer is Yes, then sort in ascending order. ' If iAnswer = vbYes Then If UCase$(Sheets(j).Name) > UCase$(Sheets(j + 1).Name) Then Sheets(j).Move After:=Sheets(j + 1) End If ' ' If the answer is No, then sort in descending order. ' ElseIf iAnswer = vbNo Then If UCase$(Sheets(j).Name) < UCase$(Sheets(j + 1).Name) Then Sheets(j).Move After:=Sheets(j + 1) End If Next j Next i End Sub
Please consider:
Be polite. Thank those who have helped you. Then Click on the star icon in the lower left part of the contributor's post and add Reputation. Cleaning up when you're done. If you are satisfied with the help you have received, then Please do Mark your thread [SOLVED] .
Another code (I think for Mr. Jbeaucaire)
Sub SortSheets() Dim Arr As Variant, vTemp As Variant, WS As Worksheet Dim I As Long, J As Long, buf As String For Each WS In Worksheets buf = buf & "," & WS.Name Next WS Arr = Split(Mid(buf, 2, Len(buf)), ",") For I = LBound(Arr, 1) To UBound(Arr, 1) - 1 For J = I + 1 To UBound(Arr, 1) If Arr(I) > Arr(J) Then vTemp = Arr(I) Arr(I) = Arr(J) Arr(J) = vTemp End If Next J Next I For I = LBound(Arr, 1) To UBound(Arr, 1) Sheets(Arr(I)).Move After:=Sheets(Sheets.Count) Next I End Sub
or maybe so
Sub tttt() Dim wsh As Worksheet, i As Long With CreateObject("System.Collections.ArrayList") For Each wsh In ThisWorkbook.Worksheets .Add wsh.Name Next .Sort For i = 0 To .Count - 2 Sheets(.Item(i + 1)).Move , Sheets(.Item(i)) Next End With End Sub
Thanks Mr. Nilem for the code
and may be
Sub Sort_Sheets() Dim Sh_Name(99), Nw_Sh(99) As Variant, X As Long, I As Long, J As Long Dim exchg X = Worksheets.Count For I = 1 To X Sh_Name(I) = Sheets(I).Name Nw_Sh(I) = Sh_Name(I) Next I For I = 1 To X For J = I + 1 To X If Nw_Sh(J) < Nw_Sh(I) Then exchg = Nw_Sh(J): Nw_Sh(J) = Nw_Sh(I): Nw_Sh(I) = exchg Next J Next I For I = X To 1 Step -1 Sheets(Nw_Sh(I)).Move Before:=Sheets(1) Next I End Sub
Hello JEAN1972,
Just for fun. Here is another macro that will sort the worksheets either in ascending (a to z) or descending (z to a) order. This code is a little longer but uses a modified bubble sort which is twice as fast as a normal bubble sort. Doesn't mean much when sorting worksheets but is important when using it to sort lists.
Sub SortWorksheets(Optional ByVal ZtoA As Boolean) Dim LB As Long Dim J As Long Dim vList As Variant Dim Sorted As Boolean Dim Temp As Variant Dim UB As Long ' Written: July 15, 2016 ' Author: Leith Ross ' Summary: Sorts worksheets in ascending or descending alpahbetical order. ' Default is ascending order (ZtoA is False). ReDim vList(1 To ThisWorkbook.Worksheets.Count) For n = 1 To ThisWorkbook.Worksheets.Count vList(n) = ThisWorkbook.Worksheets(n).Name Next n LB = LBound(vList) UB = UBound(vList) Do Sorted = True For J = LB To UB - 1 If ZtoA Xor StrComp(vList(J), vList(J + 1), vbTextCompare) = 1 Then Temp = vList(J + 1) vList(J + 1) = vList(J) vList(J) = Temp Sorted = False End If Next J UB = UB - 1 Loop Until Sorted Or UB < 1 For n = 1 To ThisWorkbook.Worksheets.Count - 1 ThisWorkbook.Worksheets(vList(n)).Move Before:=ThisWorkbook.Worksheets(n) Next n End Sub
Sincerely,
Leith Ross
Remember To Do the Following....
1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.2. Thank those who have helped you by clicking the Star below the post.3. Please mark your post [SOLVED] if it has been answered satisfactorily.
Old Scottish Proverb...
Luathaid gu deanamh maille! (Rushing causes delays!)
See if this is how you wanted.
Sub test() Dim ws As Worksheet, i As Long With CreateObject("System.Collections.SortedList") For Each ws In Sheets .Item(GetSortVal(ws.Name)) = ws.Name Next For i = .Count - 1 To 0 Step -1 Sheets(.GetByIndex(i)).Move before:=Sheets(1) Next End With End Sub Function GetSortVal(ByVal txt As String) As String Dim i As Long, m As Object With CreateObject("VBScript.RegExp") .Global = True .Pattern = "\d+(\.\d+)?" If .test(txt) Then For i = .Execute(txt).Count - 1 To 0 Step -1 Set m = .Execute(txt)(i) txt = Application.Replace(txt, m.firstindex + 1, m.Length, _ Format$(m.Value, String(12, "0") & "," & String(10, "0"))) Next End If End With GetSortVal = txt End Function
Try
Sub Sort_Sheets_Winon() Dim I As Integer Dim J As Integer Dim iAnswer As VbMsgBoxResult iAnswer = MsgBox("Sort Sheets In Ascending Order?" & Chr(10) & "Clicking No Will Sort In Descending Order", vbYesNoCancel + vbQuestion + vbDefaultButton1, "Sort Worksheets") For I = 1 To Sheets.Count For J = 1 To Sheets.Count - 1 If iAnswer = vbYes Then If UCase$(Sheets(J).Name) > UCase$(Sheets(J + 1).Name) Then Sheets(J).Move After:=Sheets(J + 1) End If ElseIf iAnswer = vbNo Then If UCase$(Sheets(J).Name) < UCase$(Sheets(J + 1).Name) Then Sheets(J).Move After:=Sheets(J + 1) End If End If Next J Next I End Sub
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks