PDA

View Full Version : [SOLVED:] Remove all before certain sentence



had1015
08-26-2016, 12:01 AM
Hi I need a macro to delete everything from the beginning of my document up to certain words

gmayor
08-26-2016, 01:03 AM
Simple enough


Sub DeleteToPhrase()
Const strPhrase As String = "the phrase to find"
Dim oRng As Range
Set oRng = ActiveDocument.Range
With oRng.Find
Do While .Execute(FindText:=strPhrase, MatchCase:=True)
oRng.Collapse 1
oRng.start = ActiveDocument.Range.start
oRng.Text = ""
Exit Do
Loop
End With
lbl_Exit:
Exit Sub
End Sub

had1015
08-26-2016, 09:27 AM
Thank you VBAX Master for your code. It works very well for me.

gmaxey
08-26-2016, 05:38 PM
Here is another way:


Sub DeleteToPhrase()
Const strPhrase As String = "the phrase to find"
Dim oRng As Range
Set oRng = ActiveDocument.Range
oRng.End = InStr(oRng.Text, strPhrase) - 1
If Len(oRng) > 1 Then oRng.Delete
lbl_Exit:
Exit Sub
End Sub