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.
Written by Allen Wyatt (last updated January 30, 2023)
This tip applies to Word 2007, 2010, 2013, 2016, 2019, and Word in Microsoft 365
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:
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.
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!
You get a document from a colleague and you notice that each paragraph starts with a tab character. Here are a couple of ...
Discover MoreOne of the more common symbols that people need to use in their writing is the degree symbol, typically used after a ...
Discover MoreWhen creating tracking documents in Word, you may need to come up with a series of dates in the document. You can type ...
Discover MoreFREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
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.
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.
Visit the WordTips channel on YouTube
FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2024 Sharon Parq Associates, Inc.
Comments