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

Page Ranges in a TOC

It is easy to generate a table of contents for a document, and that TOC can contain page number references for each ...

Discover More

Making Text Boxes Appear in a Printout

Add some text boxes to your document, and you might be surprised if they don't appear when you print the document. If ...

Discover More

Rounding Up to a Value Ending in 9

Need to round values up to the next value that ends in 9? There are a number of ways you can accomplish the task through ...

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

Removing Tabs Used to Indent a Paragraph

You get a document from a colleague and you notice that each paragraph starts with a tab character. Here are a couple of ...

Discover More

Entering a Degree Sign

One of the more common symbols that people need to use in their writing is the degree symbol, typically used after a ...

Discover More

Generating a List of Dates

When creating tracking documents in Word, you may need to come up with a series of dates in the document. You can type ...

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 two more than 7?

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.