Hello Excellers, I am happy to bring you another Excel Macro Tip today in the #macromonday blog post series. This macro is one that I use a lot at work – Sorting my Excel sheets alphabetically by name. I generate a lot of reports, and those reports sometimes contain a lot of worksheets to represent salespeople. It is really handy to be able to sort these worksheets by name with a click rather than rearrange them if a salesperson leaves or a new one joins the company. Or if you have spreadsheet users that prefer everything sorted, all of the time. So, let’ get on with another Excel tip.
What Does The Macro Do?
This Macro will iterate through each of the sheets in your Excel workbook and sort them alphabetically. It actually compares the current worksheet name to the previous worksheet, if the previous is greater alphabetically to the current then it moves the sheet in front of it. Excel works its way through all of the worksheet names until completed and sorted. Easy huh?. Much easier than manually moving worksheets around your Excel workbook.
How Does It Work?
Preparation Before Writing The VBA Code
First, you will need to open the Visual Basic Editor. There are two ways to do this. Either by hitting ALT +F11 or selecting the Developer Tab | Code Group | Visual Basic. Both methods have the same result. You then have a choice, you can either save this macro in your Personal Macro Workbook, or in your current workbook. What’s the difference?. If you save the macro in your Personal Macro workbook it will be available for use in any of my Excel workbooks. If you store it in the current workbook then use is restricted to that workbook. It makes sense to store this particular Excel macro in the Personal Workbook as it could be useful in multiple Excel workbooks I want to sort alphabetically.
Learn More About Your Personal Macro Workbook (PMW)
If you want to read more about your Excel PMW then check out my blog posts below.
Starting The Macro
We need start off the macro by inserting a New Module. Do this by selecting the Personal.xlsb workbook, then Insert Module. Type Sub then the name of your macro. In this example, I have called it simply Sort_Worksheets. Notice that Excel will automatically enter the end text End Sub to end the Sub Routine. We simply need to enter the rest of the code between these two lines.
The next step in the code is to declare a couple of variables. This ensures that Excel creates a memory container for each of them.
Beginning The Iteration Through The Worksheets.
Excel starts iteration counting for both variables. The PreviousSheetIndex is set to 1 behind the CurrentSheetIndex.
Setting The Sheet Names To Uppercase, the Sorting The Sheets
Excel uses the UCase to get both sheet names in upper case to ensure that all sheets names sorted evenly. Only if the Previous Sheet name is greater than the Current Sheet name will the Current Sheet be moved to a position before the Previous Sheet using the Move method.
Looping Through All And Increasing The Increments.
The looping begins again. Every iteration increases the increments of both variables by 1 number until the last worksheet is reached. The macro ends when all iterations have been carried out.
Want The Code?
What Next? Want More Tips?
So, if you want more tips then sign up for my Monthly Newsletter where I share 3 Tips on the first Wednesday of the month and receive my free Ebook, 30 Excel Tips.
If you want to see all of the blog posts in the Macro Monday series. Click on the link below
Do You Need Help With An Excel Problem?.
Finally, I am pleased to announce I have teamed up with Excel Rescue, where you can get help FAST. All you need to do is choose the Excel task that most closely describes what you need to be done. Above all, there is a money back guarantee and similarly Security and Non-Disclosure Agreements. Try It!. Need Help With An Excel VBA Macro?. Of course, you don’t need to ask how to list all files in a directory as it is right here for free.