vba search and delete code doesn't work

  • This code does not work. I am trying to remove some page breaker lines. I am using this code to search the word caepipe and select a couple lines below that then delete, and hope that the loop would continue until it finish removing those lines. What could be wrong? The code runs fine, but just doesn't remove anything. Thanks.


    'Look for Row Numbers
    With Worksheets("Coordinates")
    startRow = Worksheets("a").Columns("B").Find(What:="coordinates", _
    LookIn:=xlValues, LookAt:=xlWhole).Row
    startRow = startRow + 4
    stopRow = Worksheets("a").Columns("B").Find(LargestNode, _
    LookIn:=xlValues, LookAt:=xlWhole).Row
    ' Copy and Paste Results to New Location
    Worksheets("a").Range("B" & startRow & ":F" & stopRow).Copy _
    Destination:=Worksheets("Coordinates").Range("A1")
    ' Delete Useless Info Between data
    CoordinateLastRow = ActiveSheet.UsedRange.Rows.Count
    For CoordinateStartRow = 1 To CoordinateLastRow Step 1
    If Cells(CoordinateStartRow, 1) = "Caepipe" Then
    CoordinateEndRow = CoordinateStartRow + 7
    Worksheets("Coordinates").Range("A" & CoordinateStartRow & ":E" & CoordinateEndRow).Select
    Selection.Delete
    Else: End If
    Next CoordinateStartRow
    End With

  • Re: vba search and delete code doesn't work


    I'm not sure if it will work, but when deleting data, I've found it works best to step backwards - so maybe give:


    Code
    For CoordinateStartRow = CoordinateLastRow To 1 Step -1
    If Cells(CoordinateStartRow, 1) = "Caepipe" Then
    CoordinateEndRow = CoordinateStartRow + 7
    Worksheets("Coordinates").Range("A" & CoordinateStartRow & ":E" & CoordinateEndRow).Select
    Selection.Delete
    Else: End If
    Next CoordinateStartRow


    Hope this helps. Please be sure to save before running this macro, though, as I did not test anything and I don't want you to lose valuable data.

  • Re: vba search and delete code doesn't work


    Here is one i use and it works great..


Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!