Delete Rows down to a String

pioshelby1980com

New Member
Joined
Jan 2, 2024
Messages
17
Office Version
  1. 365
Platform
  1. Windows
Hi,

I have an excel sheet. I want to clear the contents from row 6 and then delete all the rows below that row until the row where the string "Hello World" is found in column B.

When I run the code, it clears the contents of row 6, as I want, but then it leave two of the rows between row 6 and the row that contains the string "Hello World" and then deletes rows further down the sheet instead. I can't see what the issue is. Can anyone help please?

VBA Code:
Sub ClearContentsAndDeleteRows()
    Dim ws As Worksheet
    Dim startRow As Long, endRow As Long
    Dim endString As Long


    ' Set the target worksheet
    Set ws = ActiveSheet ' Change this if you want to specify a different sheet


    ' Find the row where "Total Invoiced To Date..." is located in Column B
    On Error Resume Next
    'endRow = ws.Columns(2).Find("Hello", , xlValues, xlWhole).Row
    'endString = "Hello"
    endRow = Application.WorksheetFunction.Match("Hello", ws.Columns(2), 0)
    On Error GoTo 0


    ' Check if the string was found
    If endRow > 0 Then
        ' Clear contents from row 6
        ws.Rows(6).ClearContents


        ' Delete all rows below row 6 until the row with the target string is found
        For startRow = 7 To endRow - 1
            ws.Rows(startRow).Delete
        Next startRow
    Else
        MsgBox "String not found in Column B.", vbExclamation
    End If
End Sub
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Try:
VBA Code:
Sub ClearContentsAndDeleteRows()
    Application.ScreenUpdating = False
    With ActiveSheet
        .Rows(6).ClearContents
        .Rows("7:" & Range("B:B").Find("Hello World").Row).Delete
    End With
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Easily done. Note the number in red.
Rich (BB code):
Sub ClearContentsAndDeleteRows()
    Application.ScreenUpdating = False
    With ActiveSheet
        .Rows(6).ClearContents
        .Rows("7:" & Range("B:B").Find("Hello World").Row - 1).Delete
    End With
    Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,908
Messages
6,175,307
Members
452,633
Latest member
DougMo

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