English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I have multiple worksheets in my MS excel file. I want to sort them in ascending or descending order.

2007-03-20 00:38:14 · 3 answers · asked by agu_pta2003 1 in Computers & Internet Other - Computers

3 answers

1. Copy the code below, starting with Option Explicit.
2. Open the Excel file in which you want to use the code.
3. Hit Alt+F11 to open the Visual Basic Editor (VBE) window.
4. From the menu, choose Insert-Module.
5. Paste the code into the code window at right.
6. Close this window by hitting small x in upper right corner of screen.

Now in your spreadsheet, to sort
Hit Tools-Macro-Macros and double-click SortWorksheets.

Option Explicit

Sub SortWorksheets()

Dim N As Integer
Dim M As Integer
Dim FirstWSToSort As Integer
Dim LastWSToSort As Integer
Dim SortDescending As Boolean

SortDescending = False

If ActiveWindow.SelectedSheets.Count = 1 Then

'Change the 1 to the worksheet you want sorted first
FirstWSToSort = 1
LastWSToSort = Worksheets.Count
Else
With ActiveWindow.SelectedSheets
For N = 2 To .Count
If .Item(N - 1).Index <> .Item(N).Index - 1 Then
MsgBox "You cannot sort non-adjacent sheets"
Exit Sub
End If
Next N
FirstWSToSort = .Item(1).Index
LastWSToSort = .Item(.Count).Index
End With
End If

For M = FirstWSToSort To LastWSToSort
For N = M To LastWSToSort
If SortDescending = True Then
If UCase(Worksheets(N).Name) > UCase(Worksheets(M).Name) Then
Worksheets(N).Move Before:=Worksheets(M)
End If
Else
If UCase(Worksheets(N).Name) < UCase(Worksheets(M).Name) Then
Worksheets(N).Move Before:=Worksheets(M)
End If
End If
Next N
Next M

End Sub

2007-03-20 13:25:46 · answer #1 · answered by billy b 3 · 0 0

I am not sure Excel sorts them for you but you can do it manually by clicking on the tab and moving the sheet to where you want. That way you can sort them yourself. Hope this helps a little.

2007-03-20 00:46:21 · answer #2 · answered by Anonymous · 0 0

You need a macro. I got it somewhere. Email me for it.

2007-03-20 08:09:18 · answer #3 · answered by unnga 6 · 0 0

you highlight your cells then go to Data/ Sort

2007-03-20 00:45:43 · answer #4 · answered by Anonymous · 0 0

fedest.com, questions and answers