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

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
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,223,888
Messages
6,175,219
Members
452,619
Latest member
Shiv1198

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