Apply macro to all work sheets

tmwsiy

New Member
Joined
Sep 16, 2024
Messages
4
Office Version
  1. 365
Platform
  1. Windows
Hi guys, i'm new to macros, I've searched and tried to get this on my own, but here i am. I have a workbook with sheets that have a Timestamp column. I recorded a macro to find a cell by timestamp, select that row, select all rows above up to row 2, and delete:

VBA Code:
Sub trim()
'
' trim Macro
'
' Keyboard Shortcut: Ctrl+Shift+Y
'
    Cells.Find(What:="10-Sep-24 6:00 AM CDT", After:=ActiveCell, LookIn:= _
        xlFormulas2, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
        xlNext, MatchCase:=False, SearchFormat:=False).Activate
    Rows("361:361").Select
    Range(Selection, Selection.End(xlUp)).Select
    Rows("2:361").Select
    Range("A361").Activate
    Selection.Delete Shift:=xlUp
End Sub

Not all of the sheets are the same, but they all have the search parameter. When I run it, creates the range by cell number, not the search parameter. Is there a solution to create dynamic range to delete instead of the static range that is being created?

I also need to apply this to all sheets in the current workbook.
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Maybe...

VBA Code:
Sub trim()
    '
    ' trim Macro
    '
    ' Keyboard Shortcut: Ctrl+Shift+Y
    '
    Dim i As Long, sht As Worksheet

    For Each sht In ActiveWorkbook.Worksheets
        i = 0

        On Error Resume Next
        i = sht.Cells.Find(What:="10-Sep-24 6:00 AM CDT", After:=sht.Cells(2, 1), LookIn:= _
                           xlFormulas2, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
                           xlNext, MatchCase:=False, SearchFormat:=False).Row
        On Error GoTo 0
        
        If i <> 0 Then
            sht.Rows("2:" & i).Delete Shift:=xlUp
        End If
    
    Next

End Sub
 
Upvote 0
Testing this out right now, looks good so far, organizing some 1500 sheets for this. You saved me Mark, thank you so much!
 
Upvote 0

Forum statistics

Threads
1,224,811
Messages
6,181,081
Members
453,021
Latest member
Justyna P

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