PDA

View Full Version : Solved: Find text and delete entire row and row above



aiya0834
02-22-2013, 12:46 PM
Im new with VBA so would appreciate any help. Im trying to search for a text ("Texas") within a Word document and delete the entire row as well as the row above it. I think Ive got the 1st part but dont know how to delete the row above the text. This is what I have so far..thanks~!!

Sub Delete()
Application.ScreenUpdating = False
Selection.HomeKey Unit:=wdStory
With Selection.Find
.ClearFormatting
.Wrap = wdFindStop
.Text = "Texas"
Do While .Execute
ActiveDocument.Bookmarks("\Line").Range.Delete
Loop
End With
Application.ScreenUpdating = True
End Sub

fumei
02-22-2013, 05:24 PM
By "row", do you really mean paragraph.

aiya0834
02-25-2013, 11:36 AM
No, I mean the row or line above it, so I want to delete the row/line that the text appears as well as the row/line above that text. thanks!

aiya0834
02-25-2013, 11:36 AM
By "row", do you really mean paragraph.
No, I mean the row or line above it, so I want to delete the row/line that the text appears as well as the row/line above that text. thanks!

fumei
02-25-2013, 04:02 PM
Ah...so you DO mean paragraph.

So, the text is in a table (i.e. with rows). Because if it is is NOT in a table, then "row" means nothing.

fumei
02-25-2013, 05:49 PM
However, you may want to try just adding a second Delete:
Sub DeleteV2()
Application.ScreenUpdating = False
Selection.HomeKey Unit:=wdStory
With Selection.Find
.ClearFormatting
.Wrap = wdFindStop
.Text = "Texas"
Do While .Execute
ActiveDocument.Bookmarks("\Line").Range.Delete
ActiveDocument.Bookmarks("\Line").Range.Delete
Loop
End With
Application.ScreenUpdating = True
End Sub

aiya0834
02-28-2013, 10:12 AM
However, you may want to try just adding a second Delete:
Sub DeleteV2()
Application.ScreenUpdating = False
Selection.HomeKey Unit:=wdStory
With Selection.Find
.ClearFormatting
.Wrap = wdFindStop
.Text = "Texas"
Do While .Execute
ActiveDocument.Bookmarks("\Line").Range.Delete
ActiveDocument.Bookmarks("\Line").Range.Delete
Loop
End With
Application.ScreenUpdating = True
End Sub
Yes, it works! Thank you so much!! and yes you were right, I meant paragraph and not "Table". This is going to save me hours from doing "monkey-like" work as I have massive documents that need to be edited. Thanks again!!

fumei
02-28-2013, 02:28 PM
Well, it does work, but programmatically it is kind of a sloppy method. Th problem is that it does NOT delete paragraphs. Therefore it could make some unintended actions.

macropod
02-28-2013, 05:56 PM
So really, all that was needed was:
Sub DeleteV3()
Application.ScreenUpdating = False
With ActiveDocument.Content.Find
.ClearFormatting
.Wrap = wdFindContinue
.Text = "[!^13]@Texas*^13"
.Replacement.Text = ""
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
Application.ScreenUpdating = True
End Sub

fumei
02-28-2013, 08:01 PM
Yup.

aiya0834
03-01-2013, 12:40 PM
So really, all that was needed was:
Sub DeleteV3()
Application.ScreenUpdating = False
With ActiveDocument.Content.Find
.ClearFormatting
.Wrap = wdFindContinue
.Text = "[!^13]@Texas*^13"
.Replacement.Text = ""
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
Application.ScreenUpdating = True
End Sub
Thanks to both of you!