Looping or Repeating on all worksheets
- From: sayerplayer <sayerplayer@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Fri, 15 Feb 2008 12:57:01 -0800
I want to repeat this on all worksheets, without individually selecting each
sheet and running the macro. I want the macro to run on all sheets and stop
at the end. I am using this on a weekly back order report, so the work***
names change weekly, but the name of each column is the same from A:L.
Please help, I can't seem to find info about this.
Here is what I have found so far, it works for each *** individually, but
I want it to do all sheets at once.
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 Work***
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 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
For Each sh In ThisWorkbook.Worksheets
Columns("A:L").Select
Selection.Sort Key1:=Range("F2"), Order1:=xlAscending, Key2:=Range("B2") _
, Order2:=xlAscending, Key3:=Range("H2"), Order3:=xlAscending,
Header:= _
xlGuess, OrderCustom:=1, MatchCase:=False,
Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal, DataOption2:=xlSortTextAsNumbers,
DataOption3 _
:=xlSortNormal
Next
End Sub
.
- Follow-Ups:
- Re: Looping or Repeating on all worksheets
- From: Dave Peterson
- Re: Looping or Repeating on all worksheets
- Prev by Date: Re: Recursively scraping web pages for embedded links and files
- Next by Date: Info on UserForms and command buttons
- Previous by thread: Add-in macro button assignment spontaneous change
- Next by thread: Re: Looping or Repeating on all worksheets
- Index(es):