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: Replacing the Last Comma.

Replacing the Last Comma

Written by Allen Wyatt (last updated July 10, 2021)
This tip applies to Word 97, 2000, 2002, and 2003


Rebecca is looking for a way to replace the last comma in a sentence with the word "and." She apparently needs to perform this type of edit quite a bit, and thought there might be a quick and easy way to doing the edit rather than needing to manually do it.

There is no built-in way to do this specific edit in Word, but you can create a simple macro that will search for the last comma, delete it, and then type the desired word. The following is an example of such a macro.

Sub ReplaceLastComma()
    Dim J As Integer
    Dim bRep As Boolean
    Dim sRaw As String

    Selection.Sentences(1).Select
    sRaw = Selection.Text
    bRep = False
    For J = Len(sRaw) To 1 Step -1
        If Mid(sRaw, J, 1) = "," Then
            Selection.Collapse Direction:=wdCollapseStart
            Selection.MoveRight Unit:=wdCharacter, Count:=J - 1
            Selection.Delete Unit:=wdCharacter, Count:=1
            Selection.TypeText Text:=" and"
            J = 1
            bRep = True
        End If
    Next J
    If Not bRep Then Selection.Collapse Direction:=wdCollapseStart
End Sub

The macro selects the current sentence (the one in which the insertion point is located, and then steps backwards through the text of the sentence. (The text is assigned to the variable sRaw for ease of processing.) If a comma is found, then the insertion point is positioned just before the comma in the document, the comma is deleted, and then a space and the word "and" is typed.

If you prefer that the macro not delete the comma, you can make the quick change of deleting the line that does the deletion (and changing the line before it so that the movement is "J+1" rather than "J-1"), or you can simply add a comma before the space in the text that is being typed by the macro.

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 (5401) 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: Replacing the Last Comma.

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

Retaining Explicit Formatting after Applying Styles

The formatting in a document is often a mix of styles and explicit formatting, applied over time. You may want to apply ...

Discover More

Checking for an Entry in a Cell

You may be looking for a way to have a formula determine if a particular cell has anything in it. Here's how you can find ...

Discover More

Sorting Conditional Formats Properly

Conditional formatting can be a great tool to get your data looking just the way you need. However, when you sort data ...

Discover More

Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!

More WordTips (menu)

Transposing Letters

My fat fingers sometimes result in typing letters in the wrong order. Here's a quick tool that allows you to easily ...

Discover More

Using Optional Hyphens

Adding hyphens to your document can affect the way in which Word wraps text from one line to the next. Optional hyphens, ...

Discover More

Checking for Words and Phrases

You may want to determine if a document contains a certain set of words or phrases. There are a couple of ways you can ...

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 6 - 0?

There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)


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.

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.