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

Continuing Macro Lines

Program a macro, and you can easily find that some lines get very long. If you want to shorten the lines so they are more ...

Discover More

Setting Limit Height in the Equation Editor

You can adjust the distance between the equation body and a limit line.

Discover More

Converting Text to Uppercase in a Macro

Macros are often used to process documents. If part of the processing involves making text selections uppercase, Word ...

Discover More

Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!

More WordTips (ribbon)

Preventing the Insertion of Non-Breaking Spaces

When you copy and paste information from the Internet, you may end up with a lot of non-breaking spaces in the pasted ...

Discover More

Inserting a Non-Breaking Space

In Word a non-breaking space will help you keep two words together on the same line. Here's two different ways that you ...

Discover More

How Word Handles Abbreviations

Abbreviations appear all over the place in our society. If you want to understand how Word recognizes them (which it has ...

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 2 + 2?

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.