Expand a Macro to second tab or sheet in a file

jrisebo

Active Member
Joined
Mar 15, 2011
Messages
306
Office Version
  1. 365
Platform
  1. Windows
I have a macro now that runs and sorts the data in a sheet. Call it 'Sheet1'. I want the macro to do the same thing to a second sheet, call it 'sheet2'

Here is how the code starts with 'sheet1'

Code:
ActiveWorkbook.Worksheets("sheet1").AutoFilter.Sort. _
        SortFields.Clear
...then goes on to sort.


So what does it need to say to also do sheet2?
Thanks
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Try a loop like this

Code:
Dim ws As Worksheet
For Each ws In Sheets(Array("Sheet1", "Sheet2"))
    With ws
        .AutoFilter.Sort.SortFields.Clear
        'rest of code
    End With
Next ws
 
Upvote 0

Forum statistics

Threads
1,207,423
Messages
6,078,443
Members
446,338
Latest member
AliB

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