Pat wonders if there is a quick way to alphabetize the worksheets in her workbook. There is no built-in method to do this, but it is possible to use a macro to do the sorting for you. I'll present two approaches in this tip. The first provides a simple way to do the sorting.
Sub SortSheets1() Dim I As Integer Dim J As Integer For I = 1 To Sheets.Count - 1 For J = I + 1 To Sheets.Count If UCase(Sheets(I).Name) > UCase(Sheets(J).Name) Then Sheets(J).Move Before:=Sheets(I) End If Next J Next I End Sub
This macro sorts the worksheet tabs by name in ascending order. If you want to sort them in descending order, all you need to do is change the > in the center line to <.
The macro works great if you have a relatively low number of worksheets in your workbook. If, when you run the macro, you note that it takes a great deal of time to run, you may want to use a more efficient sorting algorithm in the macro. For instance, the following is a version that reads the names of all the worksheets into an array, sorts the array using the BubbleSort algorithm, and then does the actual arranging:
Sub SortSheets2() Dim I As Integer Dim sMySheets() As String Dim iNumSheets As Integer iNumSheets = Sheets.Count Redim sMySheets(1 To iNumSheets) For I = 1 To iNumSheets sMySheets(I) = Sheets(I).Name Next I BubbleSort sMySheets For I = 1 To iNumSheets Sheets(sMySheets(I)).Move Before:=Sheets(I) Next I End Sub
Sub BubbleSort(sToSort() As String) Dim Lower As Integer Dim Upper As Integer Dim I As Integer Dim J As Integer Dim K As Integer Dim Temp As String Lower = LBound(sToSort) Upper = UBound(sToSort) For I = Lower To Upper - 1 K = I For J = I + 1 To Upper If sToSort(K) > sToSort(J) Then K = J End If Next J If I <> K Then Temp = sToSort(I) sToSort(I) = sToSort(K) sToSort(K) = Temp End If Next I End Sub
Anyone who has programmed for some time knows that BubbleSort is a good general-purpose sorting routine, but there are faster ones available. For instance, if you have quite a few worksheets, and they start out very disorganized, you may find that the QuickSort algorithm is more beneficial. All you would need to do to change the above to use QuickSort is add the QuickSort algorithm as a subroutine (you can find the algorithm in any good Visual Basic programming book) and then call the procedure from within the main SortSheets macro. (This means changing the line where BubbleSort is now called.)
There is another difference between this second macro and the first. The first macro does not pay attention to the case of the text used to name your worksheets. Thus, MyWorksheet would be viewed the same as MYWORKsheet. The second macro does pay attention to text case, and sorts accordingly. Of course, this is not a particularly big issue, since Excel doesn't pay attention to case in worksheet names, either.
Just a quick note: The BubbleSort routine shown above is general-purpose in nature. That means it will work for sorting any array, not just for sorting the names of worksheet tabs.
Note:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (13440) applies to Microsoft Excel 2007, 2010, 2013, and 2016. You can find a version of this tip for the older menu interface of Excel here: Alphabetizing Worksheet Tabs.
Program Successfully in Excel! John Walkenbach's name is synonymous with excellence in deciphering complex technical topics. With this comprehensive guide, "Mr. Spreadsheet" shows how to maximize your Excel experience using professional spreadsheet application development tips from his own personal bookshelf. Check out Excel 2013 Power Programming with VBA today!
Excel keeps a full set of properties related to workbooks. When it comes to worksheets, however, there is very little ...
Discover MoreWhen you add a new worksheet to a workbook, it receives a meaningful name such as "Sheet4" or "Sheet17." If you want to ...
Discover MoreExcel allows you, in your formulas, to include references to cells on other worksheets. Those references include the name ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2016-05-07 15:52:04
John Allott
A warning in regards to sorting sheets. If you are using a formula across sheets sorting may break this.
If I do have a need to use a formula across sheets I use a approach of having a first and last sheet that are blank and put all other sheets between these, then I can sort or change order without risk of breaking the formula
2016-04-19 23:14:22
Hi ,
The macro works fine, thanks.
Is there any way to sort the worksheets as per a list that we have ( not any standard procedure etc). Suppose I am having 50 tabs in a workbook and the tab names are alphanumeric . Like BDUK02391 Jan'16 , BDUK02391 Feb'15 etc ..
Regards,
Sharad Gupta
Cma.sharadgupta@gmail.com
2016-04-17 20:25:18
Neil
Thanks for these macros, they're great! For me, the first macro did pay attention to text case, and the second macro seemed to ignore any sheet which starts with an upper case letter. The first one worked better for me.
2016-04-16 17:00:07
RAK_da_Pira
A version of the QuickSort algorithm, coded in VBA (ready for insertion into a VBA project) can be found at:
http://stackoverflow.com/questions/152319/vba-array-sort-function
2016-04-16 15:45:12
stefan
this worked great! is there any way to sort a subset of worksheets by name (contiguous of course!) i've searched to no avail.
s
2016-04-16 06:06:37
Sorinola Oladapo
This is interesting...thanks a lot
Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2022 Sharon Parq Associates, Inc.
Comments