sort selected worksheets A-Z by means of a macro

Dear Experts:

I would like to run a macro that performs the following action:

The currently contiguously selected worksheets are to be sorted A-Z.

Help is much appreciated. Thank you very much in advance.

Regards, Andreas
Andreas HermleTeam leaderAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Roy CoxGroup Finance ManagerCommented:
Try this
Option Explicit

Sub SortWorksheets()

''/// sort worksheets in a workbook in ascending order

    Dim iCnt As Integer, iX As Integer, iJ As Integer

    Application.ScreenUpdating = False

    iCnt = Worksheets.Count

    If iCnt = 1 Then Exit Sub

    For iX = 1 To iCnt - 1

        For iJ = iX + 1 To iCnt

            If Worksheets(iJ).Name < Worksheets(iX).Name Then

                Worksheets(iJ).Move Before:=Worksheets(iX)

            End If

        Next iJ

    Next iX

End Sub

Open in new window

Andreas HermleTeam leaderAuthor Commented:
Hi Roy,

thank you very much for your swift and professional help. As a matter of fact I would sort just the selected ones and leave the non-selected alone.

Is this possible?

Regards, Andreas
NorieAnalyst Assistant Commented:

Are the selected sheets contiguous?
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Roy CoxGroup Finance ManagerCommented:
This code here should do that

pls try
Sub SortWorksheets()
    Dim N As Integer
    Dim M As Integer
    Set sel = ActiveWindow.SelectedSheets
    For M = 1 To sel.Count
        For N = M To sel.Count
            If UCase(sel.Item(N).Name) < UCase(sel.Item(M).Name) Then
                sel.Item(N).Move Before:=sel.Item(M)
            End If
        Next N
    Next M
End Sub

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Andreas HermleTeam leaderAuthor Commented:
Hi Rafael, this did the trick and all my selected sheets (contiguous) got sorted. Thank you very much.

To all the others, thank you very much for your great, professional and swift support.

Regards, Andreas
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.