Removing Duplicates in Word

Written by Allen Wyatt (last updated April 3, 2021)
This tip applies to Word 2007, 2010, 2013, 2016, 2019, and Word in Microsoft 365


2

Steve keeps e-mail addresses in a Word document. To find and remove duplicate addresses, he has to transfer the addresses to an Excel workbook. Steve wonders if there is a way to remove duplicates within Word.

For the purposes of this tip, I'm going to assume that each e-mail address is in its own paragraph, meaning that there is a hard return at the end of individual addresses. Also, the addresses are in regular text, not within a table.

With that in mind, the following macro can be a big help. It uses two For...Next loops to step through the individual paragraphs in a document backwards. When a duplicate paragraph is located, the duplicate is deleted. The macro does not require that the e-mail addresses be sorted.

Sub DelDupesFull()
    Dim J As Long
    Dim K As Long

    Application.ScreenUpdating = False
    With ActiveDocument
        For J = .Paragraphs.Count - 1 To 1 Step -1
            For K = .Paragraphs.Count To J + 1 Step -1
                If .Paragraphs(J).Range.Text = _
                  .Paragraphs(K).Range.Text Then _
                  .Paragraphs(K).Range.Delete
            Next K
        Next J
    End With
    Application.ScreenUpdating = True
End Sub

Note that the two paragraphs must be exactly the same. If, for instance, one paragraph has an extra space at the beginning or end or one has different capitalization, then they won't be considered the same and one won't be deleted. The macro also works on all paragraphs in a document, not just on a portion of them.

If you prefer a variation that works just on selected text, then the following will work for you:

Sub DelDupesSec()
    Dim J As Long
    Dim K As Long

    Application.ScreenUpdating = False
    With Selection
        For J = .Paragraphs.Count - 1 To 1 Step -1
            For K = .Paragraphs.Count To J + 1 Step -1
                If .Paragraphs(J).Range.Text = _
                  .Paragraphs(K).Range.Text Then _
                  .Paragraphs(K).Range.Delete
            Next K
        Next J
    End With
    Application.ScreenUpdating = True
End Sub

To use the macro, just select the paragraphs you want to affect and then run it. Nothing outside of your selected paragraphs will be modified.

I provided the possible macro-based solutions first on the assumption that removing the duplicates is something that needs to be done regularly or on multiple files. Truth be told, though, you don't need to actually use a macro. There is an approach you can use that relies on the wildcard capabilities of Word's Find and Replace capabilities. Follow these steps:

  1. Sort the paragraphs containing the e-mail addresses.
  2. Press Ctrl+H to display the Replace tab of the Find and Replace dialog box.
  3. Click the More button, if it is available.
  4. Make sure the Use Wildcards check box is selected.
  5. In the Find What box, enter "(*^13)@" (without the quote marks).
  6. In the Replace With box, enter "\1" (again, without the quote marks).
  7. Click on Replace All.
  8. Close the Find and Replace dialog box.

This works because of the way that the pattern in the Find What box (step 5) is set up. The first part, within the parentheses, finds anything (the asterisk) ending in a hard return (the ^13). The second part is the @ sign, which means "find one or more occurrences of the forgoing." Thus, it finds duplicate paragraphs that are one after the other. (This is why the first step, sorting the e-mail addresses, is necessary.) These are then replaced with whatever was in the parentheses in step 5.

This might help explain what is going on a bit better. Let's say that after sorting, your list of e-mail addresses looks like this:

1 allen@abcd.com
2 bill@efgh.com
3 bill@efgh.com
4 bill@efgh.com
5 cari@ijkl.com

Note that I added the leading numbers here to help with the explanation only; they are not a part of the list of e-mail addresses.

The Find What pattern, (*^13)@, would match paragraphs 2-4 because paragraph 2, which matches the portion of the pattern within parentheses, is repeated two more times on lines 3 and 4. (Remember that the @ symbol matches "one or more occurrences of the foregoing.") This match of three paragraphs is then replaced with what was originally matched by the *^13 portion of the Find What pattern, which means the contents of line 2. Thus, the effect is that paragraphs 2-4 are replaced with paragraph 2, and duplicates are removed.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the WordTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (13842) applies to Microsoft Word 2007, 2010, 2013, 2016, 2019, and Word in Microsoft 365.

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

Factory Default Settings for Word

Do you long for a way to reset Word to a "factory default" condition? It is almost impossible to get things to the way ...

Discover More

Missing PivotTable Data

Wonder what happened to the data behind a PivotTable? It could be in a number of places, and tracking it down could be a ...

Discover More

Adjusting Status Bar Font Size

Excel can helpfully display some statistical information in the program's Status Bar. If you cannot see all the ...

Discover More

Learning Made Easy! Quickly teach yourself how to format, publish, and share your content using Word 2013. With Step by Step, you set the pace, building and practicing the skills you need, just when you need them! Check out Microsoft Word 2013 Step by Step today!

More WordTips (ribbon)

Automatically Identifying Repeated Words

Need to find out how many times words are repeated in a document? If so, you'll appreciate the discussion in this tip ...

Discover More

Slowing Down Mouse Selection

We've all experienced the problem: You start selecting a large block of text using the mouse, and before you know it the ...

Discover More

Editing a Document with Many Pages

Working with large or long documents in Word can present some interesting challenges. The most common challenge is that ...

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}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. 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 two more than 7?

2021-04-04 06:10:39

Frank Keighley

Great tip Allen.


2021-04-03 07:21:56

Nick Wright

The problem with doing long tasks, such as replacing text in a very large document, is that Word takes an inordinate time. So a search and replace to add a tab and a character to the end of a line of text in a document that contains 60,000 lines (paragraphs) of text or to sort the text of 60,000 lines can take an age (sometimes over an hour). Excel does this sort of thing quickly. Why can't Word?


This Site

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

Videos
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.