How do I loop the following code through workbook?

vllgv

Board Regular
Joined
Oct 27, 2010
Messages
206
Code:
Sub Macro6()

Cells.Find(What:="AUTO.WHSE.", After:=ActiveCell, LookIn:= _
xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
xlNext, MatchCase:=False, SearchFormat:=False).Activate


Rows(ActiveCell.Row & ":" & (ActiveCell.Row + 2)).Delete


End Sub
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Untested code, but give it a try
Code:
Sub Macro6()
    Dim ws As Worksheet

    Application.ScreenUpdating = False
    For Each ws In ActiveWorkbook.Worksheets
        ws.Activate
        Cells.Find(What:="AUTO.WHSE.", After:=ActiveCell, LookIn:= _
        xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
        xlNext, MatchCase:=False, SearchFormat:=False).Activate
            
        Rows(ActiveCell.Row & ":" & (ActiveCell.Row + 2)).Delete
    Next ws
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Mr. Momentman,

Thank you very much for the code. This is a MrExcel problem I am solving for someone else and would like to give you credit for your efforts.

I have tested the code:

It works when there is one instance on one or more sheets.

When there are 2 or more instances on one sheet, there is no error, but it only finds the first one.

'Great day,

v
 
Upvote 0
MomentMan,

I was able to figure it out. Thanks again for your help.

Code:
Sub FindDelete()
    Dim ws As Worksheet
    Dim c As Range
    Dim c_Address As String
    On Error Resume Next
    Application.ScreenUpdating = False
    
    For Each ws In ActiveWorkbook.Worksheets
      ws.Activate
      With Range("A1:D100") 'Update range!
        Set c = .Find(What:="AUTO.WHSE.")
        If Not c Is Nothing Then
            c_Address = c.Address
            Do
              c.Rows(.Row & ":" & (.Row + 2)).Delete
              Set c = .Find(What:="AUTO.WHSE.")
            Loop While Not c Is Nothing And c.Address <> c_Address
        End If
      End With
    Next ws
    Sheets(1).Select
    Range("A1").Activate
    
    Application.ScreenUpdating = True
 End Sub
 
Upvote 0

Forum statistics

Threads
1,214,935
Messages
6,122,337
Members
449,077
Latest member
Jocksteriom

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