Please Note: This article is written for users of the following Microsoft Word versions: 97, 2000, 2002, and 2003. If you are using a later version (Word 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Word, click here: Bumping Numbers in a Document.

Bumping Numbers in a Document

by Allen Wyatt
(last updated May 25, 2016)

3

Documents often contain many words that are purposefully very similar to each other. For instance, you may have a document that references a series of part numbers, and the part numbers are all very similar to each other. Or you may reference a group of file names in which the base portion of the name is the same word, but each file name has a suffix that is a number, such as the following:

Widget01
Widget02
Widget03
Widget04
Widget05

If you ever have a need to increment the numbers within your document, the process can be very tedious and error-prone to do by hand. (Depending, of course, on the number of names you need to change.) This means that the task is a perfect candidate for being done by a macro.

As an example, the following VBA macro, BumpNumbers, will search for all instances of the word Widget followed immediately by a two-digit number. The number will then be incremented.

Sub BumpNumbers()
    Dim J As Integer
    Dim sFindText As String
    Dim sReplaceText As String

    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With

    For J = 98 To 1 Step -1
        sFindText = "Widget" & Right("00" & Trim(CStr(J)), 2)
        sReplaceText = "Widget" & Right("00" & Trim(CStr(J + 1)), 2)
        Selection.Find.Text = sFindText
        Selection.Find.Replacement.Text = sReplaceText
        Selection.Find.Execute Replace:=wdReplaceAll
    Next J
End Sub

Obviously, this macro is tailored to a specific need—the word Widget followed by a two-digit number. If you need to modify the macro to fit your numbering needs, you can do so by changing the For ... Next loop (so it doesn't go from 98 to 1) or by changing the text being searched for (which is assigned to the sFindText variable).

WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (814) applies to Microsoft Word 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Word (Word 2007 and later) here: Bumping Numbers in a Document.

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

Determining a Random Value

If you need to determine a random value in a macro, you can do so using the Rnd function. This tip presents the syntax and ...

Discover More

Engineering Calculations

Need to normalize your data in some way so that all your values are in a given format? This tip presents a number of ...

Discover More

Generating a List of Unique Words

Need to grab a list of unique words appearing in a document? You can tap the power of VBA's Words collection to perform the ...

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 (menu)

Jumping to the Start or End of a Document

When creating macros, it is often necessary to move the insertion point around the document so that text can be processed in ...

Discover More

Determining How Many Windows are Open

You can open multiple documents at the same time in Word, and each document occupies its own document window. Here's a way ...

Discover More

Determining the Number of Pages in Your Document

If your macro needs to know how many pages are in your document, you can use the Information method to get the desired info. ...

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 8Mpixels. 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 eight less than 9?

2016-06-13 01:24:20

Sundarasrinivasan

how to find out two digit or many more any digits in word file


2014-09-15 15:55:42

Anonymous

Answer to "Ross".

The following lines of code have been tailored to fit the task needed.
Before using the following code use Microsoft Word's "Find And Replace" feature, and in the field for finding input "v", and in the field for replacing input "v00"

Sub BumpNumbers()
Dim J As Integer
Dim sFindText As String
Dim sReplaceText As String

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With

For J = 0 To 1000 Step +1
sFindText = "v" & Right("00" & Trim(CStr(J)), 2)
sReplaceText = "v" & Right("00" & Trim(CStr(J + 1)), 2)
Selection.Find.Text = sFindText
Selection.Find.Replacement.Text = sReplaceText
Selection.Find.Execute Replace:=wdReplaceAll
Next J
End Sub


2014-08-26 01:08:50

Ross

I would like to not so much bump existing numbers but put in a series of incrementing numbers in specific places in a text. For example, if I have the marker "v" scattered throughout a text I would like to add a '1' to the first one like "v 1 ", and "v 2 " at the next instance, and so forth.

An added complication - with seems to be the clincher in my experiments is that it needs to happen only within a selection of the available text in a document.

So far I have:

Sub Increment_Verse_Number()
Dim iNextVerse As Integer
Dim sReplaceText As String
iNextVerse = 1

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With

With Selection
sFindText = "v "
sReplaceText = "v" + Str$(iNextVerse) + " "
iNextVerse = iNextVerse + 1
Selection.Find.Text = sFindText
Selection.Find.Replacement.Text = sReplaceText
Selection.Find.Execute Replace:=wdReplaceOne
End With
End Sub

However on the first replace the selection is removed and whatever I can do I can't get the replace value to increment!

Thanks.


This Site

Got a version of Word that uses the menu interface (Word 97, Word 2000, Word 2002, or Word 2003)? This site is for you! If you use a later version of Word, visit our WordTips site focusing on the ribbon 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.