Sorting Worksheets According to Region

Written by Allen Wyatt (last updated March 7, 2026)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, 2021, 2024, and Excel in Microsoft 365


Koen has a workbook containing 150 worksheets, one for each company branch. He needs to sort those sheets based on the region to which each branch belongs. (There are five regions in his company.) He also notes that it would be great if the tab color for each worksheet could reflect the region, and wonders if such sorting is possible in Excel.

Yes, such sorting is possible, but it does require the use of a macro. The hardest part of creating the macro is defining how you determine which branches are in which region. Perhaps the simplest way to do this is to make sure your worksheets use a naming pattern that includes both the region and branch information. For instance, you might name the worksheets something like "Reg01-Branch123." Your macro could then step through each worksheet and do both the coloring and the sorting.

Sub SortWorksheets()
    Dim iReg As Integer
    Dim I As Integer
    Dim J As Integer
    Dim K As Integer
    Dim ws As Worksheet

    Application.ScreenUpdating = False

    ' Set tab colors
    For Each ws in Worksheets
        iReg = Val(Mid(ws.Name,4,2))
        Select Case iReg
            Case 1
                ws.Tab.Color = vbRed
            Case 2
                ws.Tab.Color = vbYellow
            Case 3
                ws.Tab.Color = vbBlue
            Case 4
                ws.Tab.Color = vbGreen
            Case 5
                ws.Tab.Color = vbCyan
            Case Else
                ws.Tab.ColorIndex = xlColorIndexNone
        End Select
    Next ws

    ' Sort the worksheets
    For I = 1 To Sheets.Count - 1
        K = I
        For J = I + 1 To Sheets.Count
            If UCase(Sheets(K).Name) > UCase(Sheets(J).Name) Then K = J
        Next J
        If K <> I Then Sheets(K).Move Before:=Sheets(I)
    Next I

    Application.ScreenUpdating = True
End Sub

The macro works through the Worksheets collection twice. On the first pass through, the region number is extracted from the worksheet's name. This is then used (in the Select Case structure) to set the tab color. If the region number is outside the range of 1-5, then no change is made to the tab color.

If your determination of what branch is in what region is more complex, then you'll need to adjust the macro accordingly. For instance, you might have a worksheet named "Region Key" that contains, in column A, the names of each of your branches and, in column B, the corresponding region number for each of those branches. Assuming that the actual data begins in row 2, you could modify the macro in the following manner:

Sub SortWorksheets2()
    Dim sTemp As String
    Dim iReg As Integer
    Dim I As Integer
    Dim J As Integer
    Dim K As Integer
    Dim ws As Worksheet
    Dim key As Worksheet

    Application.ScreenUpdating = False

    Set key = Worksheets("Region Key")

    ' Set tab colors
    For Each ws in Worksheets
        sTemp = UCase(ws.Name)
        I = 2    ' Beginning row number
        iReg = 0
        While key.Cells(I, 1) > ""
            If UCase(key.Cells(I, 1)) = sTemp Then iReg = key.Cells(I, 2)
            I = I + 1
        Wend
        Select Case iReg
            Case 1
                ws.Tab.Color = vbRed
            Case 2
                ws.Tab.Color = vbYellow
            Case 3
                ws.Tab.Color = vbBlue
            Case 4
                ws.Tab.Color = vbGreen
            Case 5
                ws.Tab.Color = vbCyan
            Case Else
                ws.Tab.ColorIndex = xlColorIndexNone
        End Select
    Next ws

    ' Sort the worksheets
    For I = 1 To Sheets.Count - 1
        K = I
        For J = I + 1 To Sheets.Count
            If UCase(Sheets(K).Name) > UCase(Sheets(J).Name) Then K = J
        Next J
        If K <> I Then Sheets(K).Move Before:=Sheets(I)
    Next I
    Sheets("Region Key").Move Before:=Sheets(1)

    Application.ScreenUpdating = True
End Sub

The biggest difference between this macro and the previous one is that this one grabs the region number from a worksheet. As the last action in the macro, the "Region Key" worksheet is moved to the very beginning of the Worksheets collection.

Note that this second macro can also result in your worksheet tabs, at the bottom of the program window, looking like a kaleidoscope of colors. The reason for this is that the tabs are sorted based on their names, not on their colors. This differs from the first macro, which effectively sorted by region and then by branch because the worksheets were named using that pattern. If you want to still use the "Region Key" approach and sort by region and then branch, you can do so by adjusting the macro a bit more:

Sub SortWorksheets3()
    Dim sTemp As String
    Dim sSortArray(499) As String
    Dim iReg As Integer
    Dim I As Integer
    Dim J As Integer
    Dim K As Integer
    Dim ws As Worksheet
    Dim key As Worksheet

    Application.ScreenUpdating = False

    Set key = Worksheets("Region Key")

    ' Set tab colors and build sort array
    J = 0
    For Each ws in Worksheets
        sTemp = UCase(ws.Name)
        I = 2    ' Beginning row number
        iReg = 0
        While key.Cells(I, 1) > ""
            If UCase(key.Cells(I, 1)) = sTemp Then iReg = key.Cells(I, 2)
            I = I + 1
        Wend

        J = J + 1
        sSortArray(J) = Right("00" & iReg, 2) & " " & ws.Name

        Select Case iReg
            Case 1
                ws.Tab.Color = vbRed
            Case 2
                ws.Tab.Color = vbYellow
            Case 3
                ws.Tab.Color = vbBlue
            Case 4
                ws.Tab.Color = vbGreen
            Case 5
                ws.Tab.Color = vbCyan
            Case Else
                ws.Tab.ColorIndex = xlColorIndexNone
                ' Force into incorrect region area for sort
                sSortArray(J) = "00 " & ws.Name
        End Select
    Next ws

    ' Sort the worksheets
    For I = 1 To Sheets.Count - 1
        K = I
        For J = I + 1 To Sheets.Count
            If UCase(sSortArray(K)) > UCase(sSortArray(J)) Then K = J
        Next J
        If K <> I Then
            Sheets(K).Move Before:=Sheets(I)
            sTemp = sSortArray(K)
            For J = K To I Step -1
                sSortArray(J) = sSortArray(J-1)
            Next J
            sSortArray(I) = sTemp
        End If
    Next I
    Sheets("Region Key").Move Before:=Sheets(1)

    Application.ScreenUpdating = True
End Sub

Note that this iteration of the macro relies upon a helper array (sSortArray) to keep track of how the names in the worksheets should be sorted.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (13710) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, 2021, 2024, and Excel in Microsoft 365.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Discovering Where Word Stores Settings

How to find your setting information in Word.

Discover More

An Automatic Two Spaces after a Period

Should you put two spaces after a sentence or just one? Ask different people and you will get different answers. To Word ...

Discover More

Understanding Color and Conditional Formatting Codes

When you create custom cell formats, you can include codes that allow you to set the color of a cell and that specify the ...

Discover More

Dive Deep into Macros! Make Excel do things you thought were impossible, discover techniques you won't find anywhere else, and create powerful automated reports. Bill Jelen and Tracy Syrstad help you instantly visualize information to make it actionable. You’ll find step-by-step instructions, real-world case studies, and 50 workbooks packed with examples and solutions. Check out Microsoft Excel 2019 VBA and Macros today!

More ExcelTips (ribbon)

Copying a Single Cell to Multiple Worksheets

If you need to copy a cell from one worksheet to a bunch of other worksheets, the work can quickly get tedious. Make the ...

Discover More

Making Multiple Worksheet Copies

If you spend a lot of time creating a worksheet, you might want to make multiple copies of that worksheet as a starting ...

Discover More

Returning a Worksheet Name

Need to know the name of the current worksheet? You can use the CELL function as the basis for finding this information ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 7 + 9?

There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)


This Site

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.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.