Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 577
Default Sort all worksheet contents with a macro

I have searched the boards and have not found what I need. Hoping you can
help. I am using the following to sort worksheet order within a workbook and
it is working perfectly.

http://www.cpearson.com/excel/sortws.htm

What I still haven't found is how to re-sort (by first column) the contents
of all worksheets. Idea situation would be to combine this with the item
referenced above.

Thanks in advance.

Scott
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Sort all worksheet contents with a macro

Sub SortWorksheets()

Dim N As Integer
Dim M As Integer
Dim FirstWSToSort As Integer
Dim LastWSToSort As Integer
Dim SortDescending As Boolean
Dim sh as Worksheet

SortDescending = False

If ActiveWindow.SelectedSheets.Count = 1 Then
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 Befo=Worksheets(M)
End If
Else
If UCase(Worksheets(N).Name) < _
UCase(Worksheets(M).Name) Then
Worksheets(N).Move Befo=Worksheets(M)
End If
End If
Next N
Next M
for each sh in Thisworkbook.Worksheets
sh.UsedRange.Sort Key1:=sh.Range("A1"), _
Order1:=xlAscending, Header:=xlYes
Next
End Sub

Adjust the header setting to fit your data.

--
Regards,
Tom Ogilvy


"Scott" wrote in message
...
I have searched the boards and have not found what I need. Hoping you can
help. I am using the following to sort worksheet order within a workbook

and
it is working perfectly.

http://www.cpearson.com/excel/sortws.htm

What I still haven't found is how to re-sort (by first column) the

contents
of all worksheets. Idea situation would be to combine this with the item
referenced above.

Thanks in advance.

Scott



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 577
Default Sort all worksheet contents with a macro

Worked like a charm. Thanks Tom!
  #4   Report Post  
Posted to microsoft.public.excel.programming
Ren Ren is offline
external usenet poster
 
Posts: 67
Default Sort all worksheet contents with a macro

Hi Tom,

i have multiple sheets from A to Z
and i need to sort column L
i used this code

Sub sort()
cells.sort Key1:=range("L2"), Header:=xlYes
End Sub

it's working for the single sheet.
but i want it for all sheets.
how to modify your code.i tried but could't get thru.

thanks in advance
Ren

"Tom Ogilvy" wrote:

Sub SortWorksheets()

Dim N As Integer
Dim M As Integer
Dim FirstWSToSort As Integer
Dim LastWSToSort As Integer
Dim SortDescending As Boolean
Dim sh as Worksheet

SortDescending = False

If ActiveWindow.SelectedSheets.Count = 1 Then
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 Befo=Worksheets(M)
End If
Else
If UCase(Worksheets(N).Name) < _
UCase(Worksheets(M).Name) Then
Worksheets(N).Move Befo=Worksheets(M)
End If
End If
Next N
Next M
for each sh in Thisworkbook.Worksheets
sh.UsedRange.Sort Key1:=sh.Range("A1"), _
Order1:=xlAscending, Header:=xlYes
Next
End Sub

Adjust the header setting to fit your data.

--
Regards,
Tom Ogilvy


"Scott" wrote in message
...
I have searched the boards and have not found what I need. Hoping you can
help. I am using the following to sort worksheet order within a workbook

and
it is working perfectly.

http://www.cpearson.com/excel/sortws.htm

What I still haven't found is how to re-sort (by first column) the

contents
of all worksheets. Idea situation would be to combine this with the item
referenced above.

Thanks in advance.

Scott




Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
new worksheet and rename from cell contents macro Henry Excel Worksheet Functions 1 September 12th 06 10:35 AM
Can I sort a table using a macro in a protected worksheet? Edgar[_2_] Excel Programming 1 June 8th 05 07:33 PM
VBA or MACRO to prevent someone from printing the contents of an Excel worksheet Marcello do Guzman Excel Programming 3 November 13th 03 06:28 PM
MACRO OR VBA CODE TO PREVENT USERS FROM PRINTING THE CONTENTS OF A WORKSHEET? Marcello do Guzman Excel Programming 1 November 8th 03 12:34 AM
Macro to Sort Automatically Based on Contents of Drop-down Box Ashleigh K. Excel Programming 1 August 25th 03 10:05 PM


All times are GMT +1. The time now is 05:06 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"