VBA: Find row containing specific text string, delete all rows after?

OddThomas

New Member
Joined
Mar 5, 2015
Messages
1
I'm working with Excel worksheets generated by financial software, in this instance reports in a set format.

These reports always contain a specific (unique) text string somewhere in column B. The number of rows before it appears is variable. There is always additional data below this row. I need to keep everything down to this row and the one below (which is blank in column B), then delete everything else that follows.

I've managed to get as far as this, which deletes a single (the first) unnecessary row, but not to do the whole range:

Code:
Sub DeleteEndRows()
Dim LastRow As Long, x As Long
Dim ws As Worksheet
Set ws = ActiveSheet

LastRow = ws.Range("B" & ws.Rows.Count).End(xlUp).Row

For x = 1 To LastRow
    If ws.Cells(x, "B") = "TEXT" Then
       ws.Rows(x + 2).Delete
    End If
Next x
 
End Sub

Total newbie to VBA (today!) so gentle hand holding much appreciated.
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Welcome to the Board!

Try this:
Code:
Sub DeleteEndRows()

Dim LastRow As Long, x As Long
Dim ws As Worksheet
Set ws = ActiveSheet

LastRow = ws.Range("B" & ws.Rows.Count).End(xlUp).Row

For x = 1 To LastRow
    If ws.Cells(x, "B") = "TEXT" Then
       ws.Rows(x + 2 & ":" & LastRow).Delete
       Exit For
    End If
Next x
 
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,084
Messages
6,123,028
Members
449,092
Latest member
ikke

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