Apply a macro to multiple sheets

agas

New Member
Joined
Mar 22, 2011
Messages
41
Hello Everyone,

I know that code looks awful but i am just very new to this.

What i want to do is make this code run for the all sheets that is open and save the file.

Code:
 ChDir "C:\Documents and Settings\aozben\Desktop\Cap Rep LongRange\1\"
    Workbooks.Open Filename:= _
    "C:\Documents and Settings\aozben\Desktop\Cap Rep LongRange\1\TK_LO_CAP_REP.xls"
        
        
    With ActiveSheet
    .Range(Selection, ActiveCell.SpecialCells(xlLastCell)).AutoFilter Field:=1, Criteria1:=Array( _
        "AYT", "DGT", "DHA", "DHQ", "DLB", "DLD", "DLI", "ETF", "FAA", "GBB", "IMT", "TIE", "TIP", _
        "YSE", "YSM", "YSP", "MAA", "YSA"), Operator:=xlFilterValues
    .AutoFilter.Range.Offset(1).SpecialCells(xlCellTypeVisible).EntireRow.Delete
    .Range("$A$3:$AG$10000").AutoFilter Field:=1
    End With
 
      ActiveWorkbook.SaveAs Filename:="C:\Documents and Settings\aozben\Desktop\Cap Rep LongRange\1\" & "AAP_TK_LO_CAP_REP.xls (" & Format(Date, "dd-mm-yyyy") & ").xls" _
  , FileFormat:=xlExcel8, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False, CreateBackup:=False

And is it possible to make it run an other code in the same macro with the same layout only the filter array is different.

Code:
    ChDir "C:\Documents and Settings\aozben\Desktop\Cap Rep LongRange\1\"
    Workbooks.Open Filename:= _
    "C:\Documents and Settings\aozben\Desktop\Cap Rep LongRange\1\TK_LO_CAP_REP.xls"
        
    With ActiveSheet
    .Range(Selection, ActiveCell.SpecialCells(xlLastCell)).AutoFilter Field:=1, Criteria1:=Array( _
        "AAP", "DGT", "DHA", "DHQ", "DLB", "DLD", "DLI", "ETF", "FAA", "GBB", "IMT", "TIE", "TIP", _
        "YSE", "YSM", "YSP", "MAA", "YSA"), Operator:=xlFilterValues
    .AutoFilter.Range.Offset(1).SpecialCells(xlCellTypeVisible).EntireRow.Delete
    .Range("$A$3:$AG$10000").AutoFilter Field:=1
    End With
    
    
    
    ActiveWorkbook.SaveAs Filename:="C:\Documents and Settings\aozben\Desktop\Cap Rep LongRange\1\" & "AYT_TK_LO_CAP_REP.xls (" & Format(Date, "dd-mm-yyyy") & ").xls" _
  , FileFormat:=xlExcel8, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False, CreateBackup:=False
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
I tried like this but couldnt make it work :(

Code:
    For Each ws In ActiveWorkbook.Worksheets
    
    Range(Selection, ActiveCell.SpecialCells(xlLastCell)).AutoFilter Field:=1, Criteria1:=Array( _
        "AYT", "DGT", "DHA", "DHQ", "DLB", "DLD", "DLI", "ETF", "FAA", "GBB", "IMT", "TIE", "TIP", _
        "YSE", "YSM", "YSP", "MAA", "YSA"), Operator:=xlFilterValues
    
    AutoFilter.Range.Offset(1).SpecialCells(xlCellTypeVisible).EntireRow.Delete
    Range("$A$3:$AG$10000").AutoFilter Field:=1
    
    Next ws
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,603
Messages
6,179,850
Members
452,948
Latest member
UsmanAli786

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