Sorting by Highlighting

by Allen Wyatt
(last updated October 4, 2014)

9

Tim has a list of items in a Word document. Each item is in its own paragraph (Enter at the end) and not in a table. Some of the items (paragraphs) have highlighting applied to them. Tim wonders if there is a way to sort the paragraphs so that the highlighted paragraphs are together. In other words, he needs to sort by highlight as the primary sort key.

Word can sort by many things, but sorting by highlighting is not one of those things. However, Find & Replace can search for highlighting. One method would be to use Find & Replace to prefix each highlighted paragraph with a unique keyword, run Find & Replace again to prefix non-highlighted paragraphs with another keyword, then sort the document using the first word as the first key so that the highlighted paragraphs are together. Afterwards run Find & Replace to remove the two special keywords. Obviously, this method is tricky to set up and requires several steps.

Another approach is to simply sort all the paragraphs, without regard to the highlighting. Once that is done, you can use a macro to move highlighted paragraphs to the start of the document. The highlighted paragraphs will appear in the sorted order they were in the document.

Sub MoveHighlightedParas()
    Dim paraMax As Long
    Dim paraStart As Long
    Dim j As Long
    Dim r As Range

    With ActiveDocument
        paraMax = .Paragraphs.Count
        paraStart = 0
        j = paraMax
        Do While j > paraStart
            If .Paragraphs(j).Range.HighlightColorIndex <> wdNoHighlight Then
                .Paragraphs(j).Range.Cut
                Set r = .Content
                r.Collapse
                r.Paste
                paraStart = paraStart + 1
            Else
                j = j - 1
            End If
        Loop
    End With
End Sub

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

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

Inserting Video into Worksheets

You can add all sorts of objects to your workbooks, including video clips. Here's the pros and cons (along with the how-to) ...

Discover More

Moving Object Anchors

When you insert an object into your document, it is anchored to a paragraph. If you want to change the paragraph to which the ...

Discover More

Creating Two Versions of the Same Document

You may often need to create two versions of the same document, one with everything and the other with a subset of what is ...

Discover More

The First and Last Word on Word! Bestselling For Dummies author Dan Gookin puts his usual fun and friendly candor back to work to show you how to navigate Word 2013. Spend more time working and less time trying to figure it all out! Check out Word 2013 For Dummies today!

More WordTips (ribbon)

Odd Sorting

Word is great at sorting simple information in tables and paragraphs. If you have more complex information (such as ...

Discover More

Sorting Dates Numerically

How you sort dates depends, in large part, on how they are formatted. This tip examines a very specific date format ...

Discover More

Changing Sort Order

When sorting information, Word follows some pretty strict rules. If you want to modify how those rules are applied, you may ...

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. 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 9 - 2?

2016-08-18 15:33:39

Andrew

In word 2010 I cannot sort a list by the last word in each paragraph. Each paragraph (separated by an Enter), has different number of words.
How can I sort this list by the LAST word in each paragraph? I need to do this with a long list of names with varying numbers of words in each paragraph.

Please give me some help.

Andy


2015-02-12 02:55:03

Goran

Hi.

This works great! Is it possible to sort the opposite way? - The highlighted paragraphs in the bottom instead of in the top?


2014-10-06 10:02:29

Jennifer Thomas

Kathyrn, I feel your pain -- I was in the same situation not so long ago. But Wyatt is right in that many of the solutions we need are simply not available in Word, so we have to write them ourselves. If you are at the point where you can sort-of-understand macros you read but don't really know how to tweak them or manipulate them in the VBA window, I'd recommend a beginning or intermediate class in VBA for Word (paid or online) -- that got me to the point where I could at least work with and make minor edits to what I copied from the 'net and has saved me a lot of time and tears ever since!


2014-10-05 12:13:51

awyatt

Kathryn,

This site (which has thousands of tips available) is about evenly mixed between tips that use VBA/macros and those that don't. For some tips macros MUST be used because that is the only way to accomplish what someone is asking -- such as with this particular tip.

That being said, the book offered when first joining the newsletter list is intended as a compendium of all the tips related to macros. If you want to learn macros from scratch -- which it sounds like you do -- then this book will be your best bet:

http://store.tips.net/T010352

It is a tutorial on how to use macros and presumes no beginning level of knowledge about macros.

-Allen


2014-10-05 11:29:12

Kathryn

Following up on Charles Stewart's question and Damear's reply "this is VBA, or Visual Basic for Applications, MS Office's very own programming language" -- This site would be better labeled as "VBA for Word" or something like that." Not so useful for people who are not programmers--I have not read one tip that tells where or how in WORD to use this VBA code. Or even mentions in the tip text that VBA is what it's talking about. (Same with the book offered if you register with the site.) Maybe I missed something?

That said, YouTube might have some videos, or Amazon a book, that may be helpful.


2014-10-04 13:34:34

Russ

It is so much easier and faster and foolproof to simply use Alt + Shift + Up/Down Arrow keys to relocate (sort) entire paragraphs when the Cursor is present anywhere within the text to be moved.


2014-10-04 07:38:17

Damear

Charles Stewart, this is VBA, or Visual Basic for Applications, MS Office's very own programming language.


2014-10-04 05:34:15

Charles Stewart

What is the programing language (or macro language) used to develop the subroutine used in this example? I have programmed in many languages so I already understand it in general but I want specific of the language structure needed to detail program myself - Where could I find a quick reference?


2014-10-04 04:36:20

Ken Endacott

If you want to retain the highlighted paragraphs in the document at the same time place copies at the start of the document, change the line:

.Paragraphs(j).Range.Cut

to:

.Paragraphs(j).Range.Copy


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.

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.