Please Note: This article is written for users of the following Microsoft Word versions: 2007, 2010, 2013, 2016, 2019, and Word in Microsoft 365. If you are using an earlier version (Word 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Word, click here: Replacing the Last Comma.

Replacing the Last Comma

Written by Allen Wyatt (last updated January 30, 2023)
This tip applies to Word 2007, 2010, 2013, 2016, 2019, and Word in Microsoft 365


4

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 (12378) applies to Microsoft Word 2007, 2010, 2013, 2016, 2019, and Word in Microsoft 365. You can find a version of this tip for the older menu interface of Word 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

Rotating Graphics Around a Different Center Point

You use the rotation handle in Word 2002 and Word 2003 to, well, rotate graphics around a center point. But if you hold ...

Discover More

Differences between Deleting, Clearing, and Cutting

When getting rid of text from your document, Word allows you to delete, clear, or cut. Here are the differences between ...

Discover More

ExcelTips: Times and Dates (Special Offer)

ExcelTips: Times and Dates focuses on the way Excel works with special time-based values. Here's how you can get a ...

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)

Understanding Smart Cut and Paste

Editing is generally made easier by a feature that Word calls smart cut and paste. If you prefer, you can turn the ...

Discover More

Replacing Random Text with Your Own Text

Word includes a little-known function that allows you to put "filler text" into your document. If you want this function ...

Discover More

Moving Breaks Quickly

Breaks in a document can be easily moved from one place to another using familiar editing techniques. The trick is to ...

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 8 - 5?

2023-01-31 10:29:08

Tomek

Oxford comma is fine in English, but in some other languages you ***never*** put comma before "and", or rather before its equivalent.
Nevertheless, the macro can be easily modified to Oxford comma.


2023-01-30 04:53:40

Zvi

Can this macro be modified to go through ALL sentences in a document?

Also if a sentence already has "and" instead of the last comma, won't it replace the last comma it finds with an "and", so there will be two "ands" at the end of the sentence?


2021-10-19 21:38:20

Roy

But... but... I thought Regular Expressions could do anything... there must be a way... the concept can't fail, even a little bit... people gush over it too much for that to be possible...

So, what's the RegEx way?

Ah, our old friend, the Oxford comma. like them when items are distinct and so need distinction. I can do without them without raging when meaning is utterly clear (that's the bar: since it IS utterly clear with them, it must also be so without them). But when lazy people use a smidge of knowledge (knowing some kind of something exists concerning them) to gloss away their laziness and ignorance... no.

I still remember my first grade teacher, Miss Henry, telling us you don't HAVE to use that last comma, that about 50% ("%" being a learning experience itself) of the time writers don't use the last one. Standing near the door to the hall (we had another in the opposite corner to the outside, to the old playground no one really used due to bees in the telephone pole holding a basketball hoop (no, er, Shinola, a telephone pole at each end of the "court") but we had it and went past those bees everyday for recess), a bit bulky she was so "near" and "mostly hiding" are fellow travelers here.

That simple thing she said, to first graders, gave me an utterly different, lifelong, perspective on learning what was being taught and how it was not, perhaps, holy writ.

Still, I like the Oxford comma. By any of its names and descriptions. Except with pandas in restaurants. (Sometimes distinction is the last thing the meaning needs.)


2018-11-26 18:16:56

Steven J. Van Steenhuyse

It appears that Rebecca has not been converted to the Oxford Comma.


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.

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.