Loop recorded macro through all worksheets

scott_86_

New Member
Joined
Sep 27, 2018
Messages
40
Office Version
  1. 365
Platform
  1. Windows
Hi,

I am trying to loop a one worksheet (name of that worksheet was Booking)recorded macro through all the sheets in the workbook.

The recording was to sort data within a range alphabetically.
No security issues I am aware of.

If anyone could amened the following code to this, that'd be great! Thanks inadvance.

--------------------------------------------------------


Sub ShootSort()
'
' ShootSort Macro

'

Range("E8:J27").Select

ActiveWorkbook.Worksheets("Booking").Sort.SortFields.Clear

ActiveWorkbook.Worksheets("Booking").Sort.SortFields.AddKey:=Range("J8:J27") _

, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal

ActiveWorkbook.Worksheets("Booking").Sort.SortFields.Add Key:=Range("E8:E27")_

, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal

With ActiveWorkbook.Worksheets("Booking").Sort

.SetRange Range("E8:J27")

.Header = xlGuess

.MatchCase = False

.Orientation = xlTopToBottom

.SortMethod = xlPinYin

.Apply

End With

End Sub



 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Maybe this
Code:
Sub MM1()
Dim WS      As Worksheet
For Each WS In Worksheets
    WS.Sort.SortFields.Clear
    WS.Sort.SortFields.Add Key:=Range("E8:E27"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With WS.Sort
        .SetRange Range("E8:E27")
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
Next WS
End Sub
 
Upvote 0
Michael,

Thanks very much, that worked great.

Is there anyway to then make an exception for a single worksheet not to be included in this loop?

I have a worksheet that comes up in in VBA as Sheet1 (Data) that I do not want the sorting code to be applied to.

Thanks in advance.
 
Upvote 0
If the sheet name is Data, try

Code:
Sub MM1()
Dim WS As Worksheet
For Each WS In Worksheets
    If WS.Name <> "Data" Then
        WS.Sort.SortFields.Clear
        WS.Sort.SortFields.Add Key:=Range("E8:E27"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        With WS.Sort
            .SetRange Range("E8:E27")
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
    End If
Next WS
End Sub
 
Upvote 0
That worked, thanks again.

I've got another query.

I've googled and tried plenty of codes, but can't get it to work:

Do you have any idea how to make all worksheets in a workbook automatically set to a specific % zoom level when opening an Excel file?

Thanks.
 
Upvote 0
Use

Code:
Sub workbook_open()
    Dim ws As Worksheet
    For Each ws In Worksheets
        ws.Select
        ActiveWindow.Zoom = 110 'change to suit zoom required
    Next ws
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,445
Messages
6,172,176
Members
452,446
Latest member
walkman99

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top