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

Modifying Proper Capitalization

The PROPER worksheet function is used to change the case of text so that the first letter of each word is capitalized. If ...

Discover More

Getting a Proper Total Page Count

Word allows you to insert dynamic page numbers within your document. With some page number formatting, though, it is ...

Discover More

Excel Dark Mode

All the rave these days seems to be displaying information in either "light mode" or "dark mode." If you are interested ...

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)

Selecting an Entire Section

Documents can be subdivided into sections, with each of them formatted differently. If you want to select all the text in ...

Discover More

Selecting an Entire Paragraph

Paragraphs are an elemental building block for documents. This tip explains the different ways you can select entire ...

Discover More

Breaking Into Sentences

Macros allow you to easily extend what you can do with Word. If you have a common editing task, that task can often be ...

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 2 + 2?

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.