how to LOOP/Repeat a Find macro

misula29

New Member
Joined
Mar 6, 2009
Messages
28
I am trying to search an entire sheet for a certain word "consolidated", and when the word appears i want the following macro to run ... but then i want this to repeat throughout the entire sheet ... please help! currently it just finds 'Consolidated" once and the macro ends

Sub PreStep1()
Cells.Find(What:="consolidated", After:=ActiveCell, LookIn:=xlFormulas,
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
ActiveCell.EntireRow.Insert
ActiveCell.Offset(1, 0).Select
ActiveCell.EntireRow.Select
Selection.Copy
ActiveCell.Offset(-1, 0).Select
ActiveSheet.Paste
ActiveCell.Select
ActiveCell.Offset(0, 3).Select
ActiveCell.FormulaR1C1 = "FEE"
ActiveCell.Offset(0, 1).Select
ActiveCell.FormulaR1C1 = "=R[1]C+R[2]C"
ActiveCell.Offset(0, 1).Select
ActiveCell.FormulaR1C1 = "=R[1]C+R[2]C"
ActiveCell.Offset(0, 1).Select
ActiveCell.FormulaR1C1 = "=R[1]C+R[2]C"
ActiveCell.Offset(0, 1).Select
ActiveCell.FormulaR1C1 = "=R[1]C+R[2]C"
ActiveCell.Offset(0, 1).Select
ActiveCell.FormulaR1C1 = "=R[1]C+R[2]C"
ActiveCell.Offset(0, 1).Select
ActiveCell.FormulaR1C1 = "=R[1]C+R[2]C"
ActiveCell.EntireRow.Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
xlNone, SkipBlanks:=False, Transpose:=False
ActiveCell.Offset(1, 0).Select
ActiveCell.EntireRow.Select
Selection.Delete
Selection.Delete

End Sub
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Make Sure to test this on a COPY of your Data.

Code:
Sub PreStep1()
   Dim lngLastCol As Long
   Dim lngLastRow As Long
   Dim lngCol As Long
   Dim lngRow As Long
   Dim rngFound As Range
   Dim rngLastCell As Range
   Dim strFirstFound As String
 
   lngLastCol = Cells.Find(what:="*", after:=Cells(1, 1), SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
   lngLastRow = Cells.Find(what:="*", after:=Cells(1, 1), SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
 
   Set rngLastCell = Cells(lngLastRow, lngLastCol)
   Set rngFound = Cells.Find(what:="consolidated", after:=rngLastCell)
 
   If Not rngFound Is Nothing Then strFirstFound = rngFound.Address
 
   Do Until rngFound Is Nothing
      lngRow = rngFound.Row
      Cells(lngRow, 4) = "FEE"
      For lngCol = 5 To 10
         Cells(lngRow, lngCol) = Cells(lngRow, lngCol) + Cells(lngRow + 1, lngCol)
      Next lngCol
      Cells(lngRow + 1, 1).EntireRow.Delete
 
      Set rngFound = Cells.FindNext(after:=rngFound)
      If rngFound.Address = strFirstFound Then Exit Do
   Loop
 
CleanExit:
   Set rngFound = Nothing
   Set rngLastCell = Nothing
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,616
Messages
6,179,908
Members
452,949
Latest member
beartooth91

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