How to Sort Excel Worksheet Tabs Alphabetically Using VBA
Question details
The user needs a way to arrange multiple worksheet tabs in an Excel workbook in alphabetical order.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- Managing a complex workbook with many sheets where finding specific tabs is difficult because they are unorganized.
- Observed behavior
- Excel does not provide a native, built-in command or single-click button to automatically sort tabs alphabetically.
Ensure you have saved a recent backup of your workbook, as actions performed by VBA macros cannot be undone using the standard Undo feature.
Use a VBA Macro to Sort Tabs Alphabetically
Implement a simple VBA script to automatically reorder all worksheet tabs from A to Z.
Since Excel lacks a direct sorting button for worksheet tabs, using a Visual Basic for Applications (VBA) macro is the most efficient workaround. This script compares the names of your tabs and rearranges them sequentially.
In your open Excel workbook, press the ALT + F11 keys simultaneously to launch the Microsoft Visual Basic for Applications window.
Click on 'Insert' in the top menu bar, then select 'Module' from the dropdown list. This opens a blank code window.
Copy and paste the following code into the blank module window: Sub SortTabs() Dim x As Integer, y As Integer For x = 1 To Sheets.Count - 1 For y = x + 1 To Sheets.Count If LCase(Sheets(y).Name) < LCase(Sheets(x).Name) Then Sheets(y).Move Before:=Sheets(x) End If Next y Next x End Sub
Press F5 on your keyboard or click the green 'Run' arrow button in the toolbar. Your worksheet tabs will immediately reorder alphabetically.

Sort Worksheet Tabs and Run Macros Seamlessly in WPS Office
WPS Spreadsheet offers robust support for VBA and macros, allowing you to automate repetitive tasks like sorting worksheet tabs just as you would in Microsoft Excel. It provides a highly familiar interface and seamless compatibility with all your existing Excel files.
- 1. Open Your Workbook: Launch WPS Spreadsheet and open the workbook containing the tabs you wish to sort.
- 2. Access the Developer Tools: Navigate to the 'Developer' tab on the ribbon and click on 'Visual Basic', or simply press ALT + F11.
- 3. Insert and Run the Macro: Insert a new Module from the top menu, paste the VBA sorting code, and press F5 to execute the sorting.
- 4. Save Your Work: Go to 'File' > 'Save As' and select 'Macro-Enabled Workbook (.xlsm)' if you wish to keep the sorting script for future use.

Frequently Asked Questions
Can I sort the tabs in descending (Z to A) order using VBA?
Yes. You can easily modify the provided VBA code by changing the less-than sign (<) to a greater-than sign (>) in the If statement. The modified line should read: If LCase(Sheets(y).Name) > LCase(Sheets(x).Name) Then.
Do I need to save my workbook in a special format after adding this macro?
If you want to keep the macro to use again later, you must save your file as an Excel Macro-Enabled Workbook (.xlsm). If you only needed to sort the tabs once and do not want to keep the code, you can save it as a standard .xlsx file, which will automatically strip the macro code upon saving.
Will this macro sort hidden worksheets as well?
The basic macro provided attempts to sort all sheets in the workbook. However, moving hidden or very hidden sheets may cause a runtime error in Excel. If your workbook contains hidden sheets, it is recommended to unhide them before running the macro, or modify the code to ignore hidden sheets.
How can I undo the tab sorting if I don't like the new order?
The standard Undo command (Ctrl + Z) does not work for actions performed by VBA macros. To revert the changes, you will either need to manually drag and drop the tabs back into their original order, or close the workbook without saving to restore the last saved state.




