Repeating macro across all worksheets

JP10

New Member
Joined
Mar 28, 2025
Messages
11
Office Version
  1. 365
Platform
  1. Windows
Hope someone can help me.

My base macro works fine when I run it on one worksheet (with any name), but when I run the macro below, which based on my research should run across all the workbooks, it only runs on the worksheet I'm on. My workbook will have a different number of worksheets, with different names everytime (it's a data extract I need to format).

Sub ExportMaster()

Dim xsheet As Worksheet
For Each xsheet In ThisWorkbook.Worksheets

Call Export
Next xsheet
End Sub

Am I missing something?

I'm wondering if it has something to do with this section of the Macro as it has 'activesheet' commands:

Selection.AutoFilter
Columns("A:A").Select
Selection.NumberFormat = "m/d/yyyy"
ActiveWorkbook.ActiveSheet.AutoFilter.Sort.SortFields.Clear
ActiveWorkbook.ActiveSheet.AutoFilter.Sort.SortFields.Add2 _
Key:=Range("A8"), SortOn:=xlSortOnValues, Order:=xlDescending, _
DataOption:=xlSortNormal
With ActiveWorkbook.ActiveSheet.AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply

Thanks in advance
 
Looks like the company security settings have disabled macros - I'll have to get it sorted at work tomorrow
 
Upvote 0
In the line:
VBA Code:
    For Each xsheet In ThisWorkbook.Worksheets
ThisWorkbook is the workbook containing the code which in your case is the Personal workbook.
Change it to be ActiveWorkbook
 
Upvote 0
Solution
In the line:
VBA Code:
    For Each xsheet In ThisWorkbook.Worksheets
ThisWorkbook is the workbook containing the code which in your case is the Personal workbook.
Change it to be ActiveWorkbook
Thank you, I feel like an idiot, it was so simple!
 
Upvote 0

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