Searching for Items in an Automatically Numbered List

by Allen Wyatt
(last updated February 6, 2021)

6

Mike has a long document that uses Word's automatic list numbering throughout it. He needs a way to search, possibly using Find and Replace, for specific numbered items in these lists. For instance, he may need to search for the second item in the lists. Mike can convert the lists to manual numbering and do the search, but he would like to find the items without having to do the conversion.

One way that you could approach this issue is to consider how you put your document together. You could, for instance, use styles for your numbered paragraphs. Specifically you could use a different style for each numbered item, such as defining styles called Item1, Item2, Item3, and so on. Once applied to your paragraphs, you could easily search for paragraphs using the appropriate styles.

If you don't want to go the style route, you would need to use a macro to find the items. You can do this if you look at each paragraph in a document and determine if it has something in its .ListString property. If it contains what you are looking for, then you know you have found a desired list item. Here's an example of a macro that will do the trick:

Sub FindParagraphNumber()
    Dim sItemNum As String
    Dim p As Paragraph
    Dim sTemp As String
    Dim iChk As Integer
    Dim iCount As Integer
    Dim bStopped As Boolean

    sItemNum = InputBox("Find what item number?")
    If sItemNum > "" Then
        iCount = 0
        bStopped = False

        ' Start going through each paragraph
        For Each p In ActiveDocument.Paragraphs
            sTemp = p.Range.ListFormat.ListString
            If sTemp Like "*" & sItemNum & "*" Then
                ' We found a match
                iCount = iCount + 1
                ' Show the paragraph
                p.Range.Select
                ActiveWindow.ScrollIntoView Selection.Range, True
                ' Ask if searching should stop
                iChk = MsgBox("Keep looking?", vbYesNo)
                If iChk = vbNo Then
                    bStopped = True
                    Exit For
                End If
            End If
        Next p

        ' Put together the results message
        sTemp = sItemNum & " was not found in any paragraph numbers"
        If iCount > 0 Then
            sTemp = sItemNum & " was found " & iCount & " time(s)"
        End If
        If bStopped Then
            sTemp = sTemp & " before the search was stopped"
        End If
        sTemp = sTemp & "."
        MsgBox sTemp
    End If
End Sub

Note that when you run the macro, you are asked for the item number you want. In Mike's case, he would enter "2", without the quote marks. Each paragraph is then inspected, and if there is a match between what Mike entered and what is in the .ListString property, then the item is selected and displayed on the screen. At that point, Mike can choose to continue looking or stop searching.

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 (13344) applies to Microsoft Word 2007, 2010, 2013, 2016, 2019, and Word in Office 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

Getting Rid of 'Mail To:' in E-mail Links

If you convert e-mail addresses to hyperlinks, you could end up with some 'mailto:' verbiage at the beginning of the ...

Discover More

Converting Codes to Characters

Character codes are the numeric values used, by a computer, to signify various alphanumeric characters. You can use the ...

Discover More

Changing Links

If your worksheet is linked to data in other worksheets, you may need to change the link from time to time. Here's how to ...

Discover More

Do More in Less Time! Are you ready to harness the full power of Word 2013 to create professional documents? In this comprehensive guide you'll learn the skills and techniques for efficiently building the documents you need for your professional and your personal life. Check out Word 2013 In Depth today!

More WordTips (ribbon)

Saving Find and Replace Operations

Want to repeat the same Find and Replace operation over and over again? Here are a couple of ways you can improve your ...

Discover More

Changing the Position of the Dollar Sign

When you receive documents created by others, it is not unusual that you'll want to edit what they've written. For ...

Discover More

When Replace Doesn't Work

Find and Replace is a great tool, but what are you to do if your find or replace doesn't work as you expect? This tip ...

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 three less than 9?

2021-02-07 09:44:11

Tomek Dluzniewski

Just for clarity about my solution below: the numbers for the list in the TOC created as I described, are explicit, that is why they can be found by Ctrl-+F.


2021-02-07 09:21:45

Tomek Dluzniewski

I checked the solution shown below in Microsoft 365 Family. It may not work or not work as well in other/earlier versions of the Office, particularly the part that refers to navigation pane, which has been updated recently by Microsoft. This solution depends on the use of styles to format the paragraphs in numbered lists as explained below.
If you select some paragraphs that are formatted using Normal style, then click on numbering in the Ribbon, Word converts these paragraphs to a numbered list and formats them using “List Paragraph” style. If this is how you created your lists this solution will work for you.
Alternatively, you can create custom styles with automatic numbering and use these styles for your numbered lists. For purposes of this solution let’s assume that you have used two styles MyList1 and Mylist2 for your lists in addition to automatic List Paragraph style.
Once you have a document more or less created, start with creating a Table Of Contents. Once the dialog opens click on Options:
(see Figure 1 below)
Remove TOC levels for all headings, scroll down, and put TOC Level 1 for List Paragraph, MyList1 and MyList2.
(see Figure 2 below)
Click OK, uncheck "Show page numbers", then click OK to create the TOC. I suggest you create the TOC at the beginning of your document followed by a page break to separate it from the rest of your document.
Now you can search for your item number, say 2, using find (Ctrl+F). This will open the navigation panel listing all found paragraphs. To avoid finding items numbered 12, 22, 202, etc. you can search to find whole words only (click on down-arrow in the search field to set options). The search will still find paragraphs in which you have “2” in the text, but it shouldn’t be an issue. Note: when looking for whole words do not include a period after the number as this will turn off this option.
Having a list of found paragraphs with some context should allow you to easily identify which one you want to go to in text. By clicking on item in that list, you can quickly navigate to the relevant item in the TOC. To go to the actual paragraph, Ctrl+click in the TOC; the entries there are actually hyperlinks to the actual paragraphs in the document.
If you edit any of your original lists, make sure to update the TOC to keep it synchronized(right click anywhere in the TOC -> Update Field. And once you’re done with editing your document simply delete this TOC.
I know it is not a simple solution, but Find and Advanced Find, as far as I know, cannot locate automatic numbering directly, may be in a future version... Having the list of the found items in the navigation pane for easy comparison may outweigh however the pain of going through setting it up.
Cheers,
Tomek Dluzniewski

Figure 1. 

Figure 2. 


2021-02-07 03:11:32

Ken Endacott

The following macro FindParagraphNumber2 will find the paragraph with the numbering text that is specified. It will work with simple and multi-level numbering. You must specify the exact numbering text including any spaces and punctuation. for example “2. 1. 3” (without the quotes), "2.1.3“, "Chapter 2”, “c)” etc. Place the cursor in any numbered paragraph of the desired list before running the macro.

Sub FindParagraphNumber2()
Dim listNo As Long
Dim numberText As String
Dim aPara As Paragraph
listNo = GetParagraphListNo
If listNo = -1 Then
MsgBox "Selected paragraph is not a member of a list"
Exit Sub
End If
numberText = ""
NextNumber:
numberText = InputBox("Number text to find?", , numberText)
For Each aPara In ActiveDocument.Lists(listNo).ListParagraphs
If StrComp(aPara.Range.ListFormat.ListString, numberText, vbTextCompare) = 0 Then
aPara.Range.Select
ActiveWindow.ScrollIntoView Selection.Range, True
If MsgBox("Find another number in same list?", vbYesNo) = vbNo Then Exit Sub
GoTo NextNumber
End If
Next aPara
MsgBox numberText & " not found." & vbCrLf & "Check punctuation and format"
End Sub

Function GetParagraphListNo() As Long
' cursor must be in any paragraph of the desired list
' returns the list number, -1 if no list
Dim alist As List
Dim aPara As Paragraph
Dim aRange As Range
Dim listNo As Long
listNo = 1
Set aRange = Selection.Paragraphs(1).Range
Do While listNo <= ActiveDocument.Lists.Count
Set alist = ActiveDocument.Lists(listNo)
For Each aPara In alist.ListParagraphs
If aPara.Range.InRange(aRange) Then
GetParagraphListNo = listNo
Exit Function
End If
Next aPara
listNo = listNo + 1
Loop
GetParagraphListNo = -1
End Function


2021-02-06 13:39:26

Ron Johnston

Allen, one of the big drawbacks in using Word's math capabilities is the inability to find and replace math symbols in a mathematical equation. Do you think there is a way of creating a search macro (similar to the macro highlighted in today's word tips) that will allow one to find a symbol, or subscripted variable, of string of Greek symbols, etc.? Thanks,

Ron (ron-john@pacbell.net)


2021-02-06 12:40:23

Tomek Dluzniewski

Ken wrote:
>The macro has shortcomings. For example if searching for 2 in the numbering it will find paragraphs with the numbers 2, 12, 20, 21 etc.
May be the macro can be modified to only look for whole words, so it will not find 12 or 20, etc.

Also, I am going to submit a comment with a different solution, which I have sent to Allen, but may be too late for him to post it.
I am busy right now, so if interested, please check in a day or two, or mark this tip to receive notifications about new comments.


2021-02-06 05:40:48

Ken Endacott

The macro has shortcomings. For example if searching for 2 in the numbering it will find paragraphs with the numbers 2, 12, 20, 21 etc. Furthermore if there are several lists then it will give results for each list. It gets worse if you have multilevel lists.

An improvement would be to search just the paragraphs that belong to the desired list rather than all lists and all paragraphs. The problem is to determine what list is the one that you want. Word doesn’t make it easy to find a list number but there are a couple of options.

1. Pick a list number and try it.
listNo = Val(InputBox("List number 1 to" & Str(ActiveDocument.Lists.Count)))

or, 2. Place the cursor in a numbered paragraph and determine the list number to which the paragraph belongs. I can post the VBA code to do this if anyone is interested.

Having got the list number then the macro can search just the paragraphs in the list with the statement:
For Each p In ActiveDocument.Lists(listNo).ListParagraphs

There is a twist here. The ListParagraphs collection is in reverse order.


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.