Please Note: This article is written for users of the following Microsoft Word versions: 97, 2000, 2002, and 2003. If you are using a later version (Word 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Word, click here: Finding and Deleting Rows.

Finding and Deleting Rows

by Allen Wyatt
(last updated March 14, 2016)

15

Sam has a document that contains some tables in which he wants to delete some rows. The rows contain specific text, which he can certainly delete by using Find and Replace, but he wants to delete the entire rows that contain that text.

There is no way to do this (delete rows) using the normal Find and Replace features of Word. Instead, you need to use a macro that will find the text and then delete the entire row. Here is a relatively simple macro that will do the job:

Sub DeleteRowWithSpecifiedText()
    Dim sText As String

    sText = InputBox("Enter text for Row to be deleted")
    Selection.Find.ClearFormatting
    With Selection.Find
        .Text = sText
        .Wrap = wdFindContinue
    End With
    Do While Selection.Find.Execute
        If Selection.Information(wdWithInTable) Then
            Selection.Rows.Delete
        End If
    Loop
End Sub

This macro first displays an input box that asks the user to specify the text to be searched for. It then starts searching for all instances of that text. If an instance is found, then the selection is checked to make sure it is within a table. If it is, then the entire row is deleted and the macro moves on to the next occurrence.

WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (3838) applies to Microsoft Word 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Word (Word 2007 and later) here: Finding and Deleting Rows.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He  is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Trimming Spaces from Strings

When processing text with a macro, you often need to remove extraneous spaces from the text. VBA provides three handy ...

Discover More

Toggling Font Assignments in a Macro

If you need to quickly switch a text selection from one typeface to another, one way you can do it is with a macro. This tip ...

Discover More

Counting Changed Words

Track Changes is a handy tool for those who need to see how a document changes over time. If you have a long document with ...

Discover More

Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!

More WordTips (menu)

Counting a Particular Word

Need to know how many times a particular word appears in a document? Here's a handy trick that will tell you the count, using ...

Discover More

Moving Footnote References Using Find and Replace

When you are editing a document, you may need to modify where the author placed footnotes relative to surrounding ...

Discover More

Replacing Text with a Graphic

The Find and Replace feature in Word provides you with some powerful tools you can use to process a document. One such tool ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] in your comment text. You’ll be prompted to upload your image when you submit the comment. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is seven more than 3?

2017-02-21 12:39:08

Miller

Thanks a ton! Works great!


2017-02-01 11:38:16

Cristian lascano

This code is very helpful but how can i change the macro to only keep what i put in the inputbox and deleted everything else


2016-12-14 00:14:30

Narendra

Thanks a lot! You saved me a couple of hours


2016-08-22 07:01:55

Amandeep Singh

Great Job!!! Saved Alots minutes.. Thanks alot


2016-08-07 06:03:30

Nanda Kumar

Excellent.
I was searching for this code since long time. Thank you Mr. Allen Wyatt.


2016-07-11 10:10:36

Theresa

Monday, July 11, 2016

I am looking for a "one-step" MACRO or MODULE process for a 1,048-page file which I need to JUST EXTRACT ONLY CERTAIN ROWS of TABLES to be inserted into an EXCEL file. This module is to FIND and then EXTRACT only two rows from ONLY "certain" tables that have the same unique identifiers (where each column and row have the same row title and its TEXTS)...
This file resulted in an MS Word 2013 document with lots of diagrams, graphics, tables, textual paragraphs spread out throughout AFTER I had merged like hundreds of Word documents into this 1 master Word document, thus each beginning section separated by a “page break” is a different document that got merged and each has its own differently titled TOP HEADER which is NOT IN A ROW. I need to pull in each of these header as well as pull only the two rows of the tables with only those unique identifiers and once they’re extracted, they can stay in another new MS WORD document.
I was thinking of using "wildcards" but then how? The length of those texts within each of those 3 rows vary...thus I guess we are looking at using VBA code to find "STRINGS"?? If that's a proper way to call it or look at it.

There are 2 columns on TOP ROW of the table: 1st column is "Verification Case ID: Testing text, testing text" 2nd column is "Verification Case Name: Testing text, testing text"

There’s 1 column to LAST ROW of the table: "Verification Objective: Testing text, testing text"

If you have an email address, I can send you an attached EXAMPLE DOCUMENT to better understand. I know this can be done, I’ve done something similar to this before but lost my macro/module and it's been 15 years since I last did VBA coding for MS WORD. Thank you kindly and in advance for any help you can muster. I need this ASAP if you can. I can be e-mailed at greatcareer4me@yahoo.com


2016-02-25 18:57:41

Tiffany

Is there a way to run this macro but skip those that have the specified word and delete those that don't?


2016-01-30 09:32:27

VeeCee

I have Word documents which contains paragraphs. The sentences are in multiple lines in a table form.

Your macro is brilliant with deleting a line containing a text. Is it possible to modify it so that if I give the first and last word of a paragraph, the whole sentence is deleted?

I found a macro which works IF I convert the table to text which, I do not want to do.

Thank you in advance.


2015-12-30 05:25:15

ken Endacott

The macro can get into an endless loop under some circumstances. The modified macro to avoid this is:

Sub DeleteRowWithSpecifiedText()
Dim sText As String

sText = InputBox("Enter text for Row to be deleted")
Selection.Find.ClearFormatting
With Selection.Find
.Text = sText
.Wrap = wdFindContinue
End With
Do While Selection.Find.Execute
If Selection.Range.Text = "" Then Exit Sub
If Selection.Information(wdWithInTable) Then
Selection.Rows.Delete
End If
Loop
End Sub

VeeCee,
replace the statement:
sText = InputBox("Enter text for Row to be deleted")

with:
sText = "My text to search for"


2015-12-30 03:01:28

VeeCee

Great.Is it possible to create a macro that does the same job without the input text? Meaning the text is in the macro and it does the job on executing the macro.


2014-12-02 21:25:15

Ricardo Cobos

thanks for the code but it didn't work for me using MS Word 2010. It Gets about 1/4 the way through my 970 page document then stops working and it never actually deletes a matching row. kind of odd.


2014-11-30 08:12:20

Tani

please I'm looking for a macro that search the text in the right cell, and replace it in the next left cell, the replacement must be limited only in the next left cell. and the same for the rest of the whole column.
best regards and many thanks


2014-07-29 04:57:05

Juha Kuivalainen

Yesterday I asked help for such simple problem because i couldn't find the parameter list that I found today. Got it fixed with MatchWholeWord :)


2014-07-28 08:39:09

Juha Kuivalainen

This helped me quite a lot at my work. I get employee lists that I have to moderate every 3 weeks for our customer.

My only problem was that the macro also picked up a particular short term from one of the names and deleted two "extra" rows. Is it possible to make the macro pick only "whole words" instead of finding the string in any form?


2014-03-19 20:00:20

Mike B

Thank you very much for this VBA code. This saved me several hours of work today. Much appreciated. :-)

BTW, this code worked for me in Work 2010.


This Site

Got a version of Word that uses the menu interface (Word 97, Word 2000, Word 2002, or Word 2003)? This site is for you! If you use a later version of Word, visit our WordTips site focusing on the ribbon interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.