# 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
###### Who is Participating?

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

x
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.

Group 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
``````
0
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
0
Analyst Assistant Commented:
Andreas

Are the selected sheets contiguous?
0
Group Finance ManagerCommented:
This code here should do that
0
Commented:
Hi,

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
``````
Regards
0

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.