Apply 1 macro to all sheets in a workbook: Why is this code not working?

ZA_Fella

New Member
Joined
Dec 8, 2023
Messages
2
Office Version
  1. 365
Platform
  1. Windows
We need to cleanup sheets, for a load file workbook. The load file will be used many times to load whatever records are needed, and the program hate rows where the data was just cleared. I have created a macro to find the last clear row in the sheet (Col B), ctr + shift + down , then delete selected rows to get rid of any lingering formats in those rows.

I have found a piece of code that allows for your macro to loop through all sheets, but when I step through the code, it stays in the active sheet. Any ideas?

VBA Code:
Sub RunMacroOnAllSheets()
    Dim ws As Worksheet
   
    ' Loop through all sheets in the active workbook
    For Each ws In ThisWorkbook.Sheets
        ' Call your macro here
        Call CleanUp
    Next ws
End Sub

Sub CleanUp()
    ' Delete all rows after the last used row
      Range("B" & Rows.Count).End(xlUp).Select
        ActiveCell.Offset(1, 0).Range("A1").Select
        ActiveCell.Rows("1:1").EntireRow.Select
        Range(Selection, Selection.End(xlDown)).Select
        Selection.Delete Shift:=xlUp
        ActiveWorkbook.Save
        Range("B" & Rows.Count).End(xlUp).Select
End Sub
 
Last edited by a moderator:

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
The second routine will only work on the active sheet. You could simply add ws.activate inside the For loop in the first code before calling the CleanUp code.
 
Upvote 0
Solution
The second routine will only work on the active sheet. You could simply add ws.activate inside the For loop in the first code before calling the CleanUp code.
Thank you so much, that solved it for me!(y)
 
Upvote 0

Forum statistics

Threads
1,215,069
Messages
6,122,959
Members
449,096
Latest member
Anshu121

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