Deleting rows between specific text

chadski778

Active Member
Joined
Mar 14, 2010
Messages
297
In work we run a regular data extract into excel from a database but a lot of the data is
not required and needs to be deleted.
Would it be possible to write a macro that can delete rows between specific strings?
I would like to delete ALL ROWS beween the following:

'Oilsafe Classification:' and 'Toxic Hazard Rating'
'Our GHS Class:' and 'Regulatory Information'
'ECN (Taiwan)' and 'REACh overall status'
'REACh overall status' and 'Physical Properties'
'Density at ambient' and 'Flash point (C)'
'Flash point (C)' and 'WGK'
AND all rows containing text AFTER 'WGK'

The pairs of text above are always in that order in the spreadsheet
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
I've not tested this, so be sure and use it on a copy of your data first

Code:
Sub deleteUnwanted()
    Dim startArray(5) As Variant, endArray(5) As Variant
    Dim f, e, i
    startArray(0) = "Oilsafe Classification:"
    startArray(1) = "Our GHS Class:"
    startArray(2) = "ECN (Taiwan)"
    startArray(3) = "REACh overall status"
    startArray(4) = "Density at ambient"
    startArray(5) = "Flash point (C)"
    
    endArray(0) = "Toxic Hazard Rating"
    endArray(1) = "Regulatory Information"
    endArray(2) = "REACh overall status"
    endArray(3) = "Physical Properties"
    endArray(4) = "Flash point (C)"
    endArray(5) = "WGK"
    Application.ScreenUpdating = False
    For i = 0 To 5
        Set f = Cells.Find(what:=startArray(i)).Offset(1)
        Set e = Cells.Find(what:=endArray(i)).Offset(-1)
        Range(f, e).EntireRow.Delete
    Next i
    Set f = Cells.Find(what:=endArray(5)).Offset(1)
    Range(f, Cells(Rows.Count, 1)).EntireRow.Delete
End Sub
I'm assuming the rows which include the given strings need to remain.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,518
Messages
6,179,248
Members
452,900
Latest member
LisaGo

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