Written by Allen Wyatt (last updated April 3, 2021)
This tip applies to Word 2007, 2010, 2013, 2016, 2019, and Word in Microsoft 365
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:
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:
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.
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!
When you copy and paste information from the Internet, you may end up with a lot of non-breaking spaces in the pasted ...
Discover MoreIn Word a non-breaking space will help you keep two words together on the same line. Here's two different ways that you ...
Discover MoreAbbreviations appear all over the place in our society. If you want to understand how Word recognizes them (which it has ...
Discover MoreFREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
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?
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.
Visit the WordTips channel on YouTube
FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2024 Sharon Parq Associates, Inc.
Comments