Run macro in multiple sheets at once

JadonR

Board Regular
Joined
Apr 15, 2009
Messages
154
Office Version
  1. 365
I have the below macro to perform a series of tasks. However, I have to click on each worksheet and run the macro and would instead like to use "With Worksheets("SheetName")" instead. I defined using "With worksheets" but the macro only performs the tasks on the active worksheet and not all three. Can someone help me?

VBA Code:
Sub AutoFill_and_Export_Test()
'
' AutoFill and Export Macro
'
Application.ScreenUpdating = False
'Clear worksheet rows 3 and below for sheet "DIM 1 Export"
[B]With Worksheets("DIM 1 Export")[/B]
    Rows("3:5000").Select
    Selection.Delete Shift:=xlUp
    End With
    With Range("A2:G2")
        .AutoFill Destination:=.Resize(Sheets("PO Worksheet").Cells(Rows.Count, "A").End(xlUp).Row - 1), Type:=xlFillDefault
    End With
   
'Clear worksheet rows 3 and below for sheet "DIM 2 Export"
With Worksheets("DIM 2 Export")
    Rows("3:5000").Select
    Selection.Delete Shift:=xlUp
    End With
    With Range("A2:G2")
        .AutoFill Destination:=.Resize(Sheets("PO Worksheet").Cells(Rows.Count, "A").End(xlUp).Row - 1), Type:=xlFillDefault
    End With

'Clear worksheet rows 3 and below for sheet "DIM 3 Export"
With Worksheets("DIM 3 Export")
    Rows("3:5000").Select
    Selection.Delete Shift:=xlUp
    End With
    With Range("A2:G2")
        .AutoFill Destination:=.Resize(Sheets("PO Worksheet").Cells(Rows.Count, "A").End(xlUp).Row - 1), Type:=xlFillDefault
    End With
Application.ScreenUpdating = True
End Sub
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
You can use this and call whatever sheets you need.


VBA Code:
Sub PreformOnWorksheets(ws As Worksheet)

    Application.ScreenUpdating = False
    With ws
        Rows("3:5000").Select
        Selection.Delete Shift:=xlUp

       With Range("A2:G2")
           .AutoFill Destination:=.Resize(Sheets("PO Worksheet").Cells(Rows.Count, "A").End(xlUp).Row - 1), Type:=xlFillDefault
       End With
    End With
    Application.ScreenUpdating = True

End Sub

call it with this code. You can set your sheet names in the collection.

Code:
Sub RunLoop()

    Dim col As Collection
    Set col = New Collection
   
    col.Add ("DIM 1 Export")
    col.Add ("DIM 2 Export")
    col.Add ("DIM 3 Export")
    'or really anything you want
   
    For i = 1 To col.Count
        Dim ws As Worksheet
        Set ws = ActiveWorkbook.Worksheets(col.Item(i))
        PreformOnWorksheets ws
       
    Next
End Sub
 
Last edited:
Upvote 0
Try:
VBA Code:
Sub AutoFill_and_Export_Test()
    Application.ScreenUpdating = False
    Dim x As Long
    For x = 1 To 3
        With Worksheets("DIM " & x & " Export")
            .Rows("3:5000").Delete Shift:=xlUp
            With .Range("A2:G2")
                .AutoFill Destination:=.Resize(Sheets("PO Worksheet").Cells(Sheets("PO Worksheet").Rows.Count, "A").End(xlUp).Row - 1), Type:=xlFillDefault
            End With
        End With
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Try:
VBA Code:
Sub AutoFill_and_Export_Test()
    Application.ScreenUpdating = False
    Dim x As Long
    For x = 1 To 3
        With Worksheets("DIM " & x & " Export")
            .Rows("3:5000").Delete Shift:=xlUp
            With .Range("A2:G2")
                .AutoFill Destination:=.Resize(Sheets("PO Worksheet").Cells(Sheets("PO Worksheet").Rows.Count, "A").End(xlUp).Row - 1), Type:=xlFillDefault
            End With
        End With
    Application.ScreenUpdating = True
End Sub

This gives me an error.
1680536985066.png
 
Upvote 0
You can use this and call whatever sheets you need.


VBA Code:
Sub PreformOnWorksheets(ws As Worksheet)

    Application.ScreenUpdating = False
    With ws
        Rows("3:5000").Select
        Selection.Delete Shift:=xlUp

       With Range("A2:G2")
           .AutoFill Destination:=.Resize(Sheets("PO Worksheet").Cells(Rows.Count, "A").End(xlUp).Row - 1), Type:=xlFillDefault
       End With
    End With
    Application.ScreenUpdating = True

End Sub

call it with this code. You can set your sheet names in the collection.

Code:
Sub RunLoop()

    Dim col As Collection
    Set col = New Collection
  
    col.Add ("DIM 1 Export")
    col.Add ("DIM 2 Export")
    col.Add ("DIM 3 Export")
    'or really anything you want
  
    For i = 1 To col.Count
        Dim ws As Worksheet
        Set ws = ActiveWorkbook.Worksheets(col.Item(i))
        PreformOnWorksheets ws
      
    Next
End Sub

So I saved both of these into Module 1 and when I run "Sub RunLoop", it still only performs on the active sheet. What am I doing wrong?
 
Upvote 0
What is the error message?
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,241
Members
449,075
Latest member
staticfluids

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