Pinterest Pixel

Sort All Worksheets By Name Using Macros In Excel

Bryan
Have a lot of worksheets, but the worksheet names are arranged in a random order? It will be a pain moving the sheets one by one to sort them out! You can sort all worksheets by nameĀ using Macros in Excel! With a simple macro, you can sort all worksheets by name in a matter of seconds.

Have a lot of worksheets, but the worksheet names are arranged in a random order? It will be a pain moving the sheets one by one to sort them out! You can sort all worksheets by nameĀ using Macros in Excel! With a simple macro, you can sort all worksheets by name in a matter of seconds.

In this article, we will explore exactly how to use Macros in Excel to sort your worksheets alphabetically. Even if you’ve never used Macros before, don’t worry—I’ll walk you through it step by step.

Key Takeaways:

  • Sorting worksheets manually can be time-consuming and error-prone.
  • Macros in Excel automate the process of sorting sheets alphabetically.
  • The Developer tab is required to access and run macros.
  • The provided macro uses a simple loop to compare and move sheet names.
  • Sorting sheets improves navigation, presentation, data consistency and saves time.

Exercise Workbook:

Download excel workbookSort-All-Worksheets-By-Name.xlsm

Benefits and Functionality of Sorting Worksheets

Why Sorting Worksheets Is Important

Before we dive into the technical steps, let’s understand why sorting worksheets is useful:

  • Easier Navigation: When your sheets are in alphabetical order, finding a particular sheet is quicker and less stressful.
  • Professional Presentation: For workbooks shared with colleagues or clients, organized worksheets make your workbook look professional.
  • Data Consistency: When analyzing or reporting data across multiple sheets, having them in a predictable order reduces errors.
  • Time Saver: Instead of manually dragging sheets, a macro can perform the sorting instantly, saving you significant time.

What This Macro Does

The macro we’ll use in this guide will:

  • Go through all worksheets in your workbook.
  • Compare their names alphabetically.
  • Automatically move them so that the names are in ascending alphabetical order.

With this macro, you no longer have to manually drag sheets or guess their correct positions. It’s a one-click solution for a common headache.

 

How to Sort All Worksheets Using Macro

Enable the Developer Tab

To use macros in Excel, you must have the Developer tab enabled. If you haven’t done this yet, follow these simple steps:

STEP 1:Ā Click on File > Options > Customize Ribbon.

STEP 2: On the right side under ā€œMain Tabs,ā€ check the Developer box. Click OK.

Now, the Developer tab will appear in your Excel ribbon, giving you access to VBA (Visual Basic for Applications) tools, including macros.

Step-by-Step Guide

STEP 1:Ā Go toĀ Developer > Code > Visual Basic

STEP 2:Ā Paste in your code andĀ Select Save. Close the window afterwards.

Sub SortAllWorksheetsByName()

Dim i As Integer
Dim j As Integer

'We use two loops to sort the sheets in ascending order
For i = 1 To Sheets.Count
For j = 1 To Sheets.Count - 1
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

Sort All Worksheets By Name Using Macros In Excel

STEP 3:Ā Let us test it out!

Open the sheet containing the data. Go toĀ Developer > Code > Macros

Make sure your macro is selected. Click Run.

Sort All Worksheets By Name Using Macros In Excel

With just one click, all of the worksheets are now sorted alphabetically!

Ā Sort All Worksheets By Name Using Macros In Excel

 

Tips and Best Practices

  • Save a Backup – Before running any macro, it’s always a good idea to save a backup of your workbook. If anything goes wrong, you can restore your original data.
  • Macro-Enabled Workbook – To save your workbook with macros, make sure to save it as Excel Macro-Enabled Workbook (.xlsm). Normal .xlsx files cannot store macros.
  • Handling Hidden Sheets – The macro will also sort hidden sheets. If you want to exclude them, you can add an If condition in the code to skip hidden sheets.
  • Custom Sorting – If you want to sort sheets in descending order, simply change the comparison operator in the code:
    If UCase$(Sheets(j).Name) < UCase$(Sheets(j + 1).Name) Then

    This will reverse the sorting order.

 

FAQs

1. What is a macro in Excel, and why do I need it for sorting worksheets?

A macro is a recorded or programmed set of instructions that automates repetitive tasks in Excel. Sorting worksheets manually can be tedious if you have many sheets. By using a macro, Excel can automatically rearrange all sheet names alphabetically in seconds. It eliminates the need for manual dragging, reduces human errors, and ensures consistency. Macros also allow you to reuse the same task in different workbooks, saving even more time in the long run.

2. How do I enable the Developer tab in Excel?

To enable the Developer tab, click File > Options > Customize Ribbon. On the right side, check the Developer box under Main Tabs. Click OK, and the Developer tab will appear in your ribbon. This tab gives access to Visual Basic for Applications (VBA) tools. Once enabled, you can create, edit, and run macros, giving you the power to automate almost any repetitive Excel task.

3. Will this macro work on hidden sheets?

Yes, the macro sorts all sheets, including hidden ones. If you want to skip hidden sheets, you can modify the code to include a visibility check. Hidden sheets are often used for calculations or backup data, so sorting them may affect their order. The default macro handles them automatically unless you customize it. This makes it versatile for workbooks with both visible and hidden worksheets.

4. Can I sort worksheets in descending order instead of ascending?

Yes, you can sort sheets in descending order by changing the comparison operator in the code. Replace > with < in the line comparing sheet names. This will reverse the sorting logic. It gives you flexibility depending on your preferred order. You can also combine this with custom prefixes or naming conventions to create a fully tailored sort.

5. Is it safe to use macros on my workbook?

Macros are safe if you create or copy them from trusted sources. Always save a backup before running macros, especially on important workbooks. Macro-enabled workbooks must be saved as .xlsm to retain the code. Following these precautions ensures you can automate tasks without risking your data. Additionally, enabling Excel’s macro security settings can help prevent malicious code from running accidentally.

If you like this Excel tip, please share it




Bryan

Bryan Hong is an IT Software Developer for more than 10 years and has the following certifications: Microsoft Certified Professional Developer (MCPD): Web Developer, Microsoft Certified Technology Specialist (MCTS): Windows Applications, Microsoft Certified Systems Engineer (MCSE) and Microsoft Certified Systems Administrator (MCSA).

He is also an Amazon #1 bestselling author of 4 Microsoft Excel books and a teacher of Microsoft Excel & Office at the MyExecelOnline Academy Online Course.

See also  Protect All Cells With Formulas Using Macros In Excel

Star 30 Days - Full Access Star

One Dollar Trial

$1 Trial for 30 days!

Access for $1

Cancel Anytime

One Dollar Trial
  • Get FULL ACCESS to all our Excel & Office courses, bonuses, and support for just USD $1 today! Enjoy 30 days of learning and expert help.
  • You can CANCEL ANYTIME — no strings attached! Even if it’s on day 29, you won’t be charged again.
  • You'll get to keep all our downloadable Excel E-Books, Workbooks, Templates, and Cheat Sheets - yours to enjoy FOREVER!
  • Practice Workbooks
  • Certificates of Completion
  • 5 Amazing Bonuses
Satisfaction Guaranteed
Accepted paymend methods
Secure checkout

Get Video Training

Advance your Microsoft Excel & Office Skills with the MyExcelOnline Academy!

Dramatically Reduce Repetition, Stress, and Overtime!
Exponentially Increase Your Chances of a Promotion, Pay Raise or New Job!

Learn in as little as 5 minutes a day or on your schedule.

Learn More!

Share to...