Heading

This is some text inside of a div block.
This is some text inside of a div block.
This is some text inside of a div block.
min read

Move sentences or words

Word • Macros • Editing
Peter Ronhovde
24
min read

This is one of those macros you feel like you’d never need, but I use it regularly.

Just push the sentence before the previous or after the next with one keystroke.

Thanks for your interest

This content is part of a paid plan.

Create the empty macro

A previous post covers creating an empty macro like the one shown below. When you’re done, you’ll have something like:

Sub MoveSentenceLeft()
' Move current sentence before the previous

End Sub

The single quote tells VBA the rest of the text on that line is a comment meant for human readers. We start our macro steps on the empty line.

Example macro moving a sentence left in a paragraph

Of course, create a second one to move the current sentence by one sentence right in the document.

What are the manual steps?

What steps would we use to move the current sentence manually?

  • Use the keyboard or mouse to select the current sentence (either way is slow).
  • Press Command+X on a Mac or Control+X in Windows to cut the sentence to the clipboard.
  • Move left one sentence.
  • Paste the sentence back into the document at the new location.

Feel like you don’t do that often? Often enough that it will help to automate it.

Some of this can be improved if you’ve applied previous articles on this blog like selecting or moving by sentences since Word already includes these commands (but without a default keyboard shortcut).

Even so, that’s still a bunch of steps we can replace with one keystroke.

Macro note

In a macro, we often don’t perform the steps exactly like we do them in Word, but this one’s close for this version.

If you’ve been following along the whole time, most of the steps have been used before in various contexts, but we’re putting them together to accomplish a different task.

Expand the Selection to include the sentence

We need to expand the Selection to encompass the current sentence, so we can cut it.

Selection.Expand Unit:=wdSentence

The Expand command uses the regular Unit constants which includes the common ones we consider when creating documents (e.g., words, sentences, paragraphs, etc.). Each constant is preceded with a “wd” to denote it as a word constant.

The Expand command is unique in that it will not grow the Selection beyond the Unit specified. This differs from how it’s used on the keyboard with F8 which grows the selection by successive unit sizes when you press the key again.

Don’t forget we specify command options using an option name, which is Unit here, followed by a := before we give the value.

Cut the Selection

Now we cut the selected sentence which is simply

Selection.Cut

This also collapses the current Selection to an insertion point.

I personally don’t like my macros using the clipboard unless need it for something specific. I’d rather the clipboard not mysteriously change on the user while they work. However, using the Selection with the clipboard is a straightforward way to create our macro today, and it saves us from implementing a more complicated, albeit slightly better, approach using Ranges.

If you’re a glutton for optimizations like this, the member version shows something closer to my approach, but this version of the macro is still fully functional.

Move to the previous sentence

We’ll use the MoveLeft method to jump to the previous sentence.

' Move backward in the document one sentence
Selection.MoveLeft Unit:=wdSentence

We again specify a sentence Unit. We omitted Count:=1 since it is the default step size. Similarly, if we wanted to move forward in the document, we would instead use

Selection.MoveRight Unit:=wdSentence

Paste the sentence

Finally paste the sentence in the new location.

Selection.Paste

Personally, I like to end the macro with the pasted content selected as a visual indicator to the user about the change, but that’s more difficult to accomplish here without some complicating steps, so we’ll leave it out here.

Gotchas

We consider what could go wrong in the macro and fix a couple cases.

Paragraph marks

I almost would leave this correction out of the free version since it complicates the macro, but it is jarring to have a paragraph split when you just want to move a sentence backward in the document.

What’s the problem?

If you Expand the selection on the last sentence of a paragraph, it automatically includes the paragraph mark. Then when we cut and paste the sentence into the new location, the paragraph mark comes with it splitting the paragraph.

Arghhh.

Get the last character

We need to exclude the paragraph mark if our macro is going to work intuitively and move only the sentence text.

How?

The last character of the last sentence selection will be the paragraph mark. In VBA and Word, the paragraph mark character is a special character with a name vbCr. We get the last character of the Selection using the Characters collection

Selection.Characters

To get the last character of the collection we just use the Last method

Selection.Characters.Last

Then we want the text of the last character for comparison. The last character is a Range, so we can just use the Text property.

Selection.Characters.Last.Text

Character comparison

Now we need to compare it to a paragraph mark character to see if we are on the last sentence of a paragraph. This uses an If statement in the mold of the following

If SomeComparison Then
' Do something if comparison is true ...
End If

The comparison is usually something like ThisValue = ThatValue, and we are checking for whether it is True before doing the steps in between.

For our case, we compare the last character to a paragraph mark character

Selection.Characters.Last.Text = vbCr

Remove paragraph mark

Now remove the paragraph mark if it exists at the end of the Selection.

If Selection.Characters.Last.Text = vbCr Then
' Remove ending paragraph mark
Selection.MoveEnd Count:=-1
End If

The If statement does nothing if the last character is anything other than a paragraph mark.

The MoveEnd command moves the End of the Selection. A negative count value moves it backward in the document removing the paragraph mark character. The default Unit is wdCharacter, so we don’t have to give it.

The member version handles the more general case of multiple empty paragraphs after the sentence.

Deleted text?

Anytime you’re cutting or deleting content, you should probably think harder about potential problems.

Fortunately, we put the text back in the macro, so not too much bad can happen except for pasting it somewhere unexpected. We can always just undo that.

Starting selections?

A typical gotcha consideration for most text-oriented macros is whether there is a starting selection or not. Then what do we do with it?

Multiple selected sentences

Having a starting selection in this macro can be a problem since we don’t want any issues if the initial selection spans any sentences. The Expand command would expand to the nearest sentence both forward and backward in the document. Multiple sentences would be selected.

While this is not a deal breaker (I personally like allowing it for my own macros), it’s also probably not what the user expects.

Unintuitive macros are bad even when you write them for yourself. Nine months down the road, you’ll inevitably forget that special condition you allowed or ignored at the time.

Just collapse the Selection

How do we solve this issue?

We should probably just collapse the Selection first. Remember the Collapse methods does nothing to an insertion point, so we know there is a well-defined starting condition with no selected text.

Selection.Collapse

In fact, any time you are unsure about how a starting selection will affect your macro, and it isn’t necessary for the macro to function properly (like adding contextual novel notes), it’s probably a good idea to just collapse the Selection (or Range) as one of the first steps of the macro.

Missing space

When moving the last sentence of the paragraph, the pasted sentence will not have an ending space because the sentence didn’t. We just need to see if a space already exists and add one if not.

This is annoying enough that we should correct for it, or the user will see little red swiggly lines under the words smashed together.

Next character with an insertion point

We again need the If statement, but here we want to know whether the “next” character is a space.

Unfortunately, when there is no selection (no spanned text in the document), the next character is actually found using the “First” character of the Selection. Confusing, I know. I wish it wasn’t like that for consistency, but that’s why you have me here.

Character comparison

Our comparison is whether the First character is not equal to a space.

Selection.Characters.First.Text <> " "

Remember the <> symbol is not equal in the sense of less than or greater than the value on the right (characters have numerical values in computer world).

Insert the space

Now we just insert a space if there isn’t one already there.

If Selection.Characters.First.Text <> " " Then
' No space, so add one between the sentences
Selection.InsertAfter Text:=" "
End If

Collapse the Selection again

Unfortunately, we need an extra Collapse command since inserting the space will leave it selected. Not a real problem, just unusual for the user, so collapse the Selection after the inserted space to avoid any quirked eyebrows months from now when the user runs the macro.

Selection.Collapse

Not a problem when moving forward

Missing spaces aren’t an issue when we are moving a sentence forward in the document.

Extra space

When moving the last sentence of the paragraph (notice how the special cases can aggravate us when creating macros), the previous sentence will probably leave a space at the end of the paragraph.

Straggling spaces annoy me in my own macros, but the macro is already getting long, and it doesn’t cause any real problems necessary to correct here. I'll leave this correction it for the member version.

Final macros

Now put the commands together.

Sub MoveSentenceLeft()
' Move the current sentence one sentence left
' Avoid any gotchas with a starting selection
Selection.Collapse

' Select the current sentence
Selection.Expand Unit:=wdSentence
' Check for last sentence of the paragraph
If Selection.Characters.Last.Text = vbCr Then
' Avoid any gotchas with a starting selection
Selection.MoveEnd Count:=-1
End If

' Move the sentence backward one
Selection.Cut
Selection.MoveLeft Unit:=wdSentence
Selection.Paste

' Ensure a space separates the sentences
If Selection.Characters.Last.Text <> " " Then
Selection.InsertAfter Text:=" "
Selection.Collapse ' unselect space
End If
End Sub

The move right version is almost identical only changing the move command and omitting the check for a space at the end.

We do, however, need separate macros since Word doesn’t allow parameters (in the parentheses) when assigning macros to keyboard shortcuts.

Sub MoveSentenceRight()
' Move the current sentence one sentence right
' Avoid any gotchas with a starting selection
Selection.Collapse

' Select the current sentence
Selection.Expand Unit:=wdSentence
' Check for last sentence of the paragraph
If Selection.Characters.Last.Text = vbCr Then
' Avoid any gotchas with a starting selection
Selection.MoveEnd Count:=-1
End If

' Move the sentence forward one
Selection.Cut
Selection.MoveRight Unit:=wdSentence
Selection.Paste
End Sub

I assigned my versions of these macros to Option+Shift+Left or Right arrow in Word for Mac or Alt+Shift+Left or Right on Windows. This isn’t perfect since I’d like these keyboard shortcuts to select the sentence instead, but it’s the best solution so far for me since I move sentences more than I need to select them.

See the member content for a solution to naturally allow dialog or parenthetical text.

Unintuitive sentence movement

I find the default sentence movement a little unintuitive, so my version adds some tweaks to make it more natural for me. Often, this occurs when moving single sentence paragraphs.

For example, when moving the first sentence of a paragraph, it feels most natural to me for it to move to the last sentence of the previous paragraph and vice versa. This is more of a personal preference, and these tweaks are more complicated than they appear at first glance, so they are relegated to member content.

Adapting for Words

The above macros moving sentences left or right in the document can be almost trivially adapted, even simplified, to move words—

What?

Hold on before you get uptight about obsessively specific macros.

Trust me a little bit here

You’ll probably find you actually want to move individual words like this sometimes. I know I do. Just one tap and it’s done. No click, drag, tap.

Just done.

Just pay attention the next time you just want to swap two words in the document, and then come back. I’ll be waiting.

Plus, the added macro fits nicely with our existing ones and doesn’t conflict with anything else, so there’s no reason to not include it. It’s a trivial amount of work to extend the sentence macros since we’ve already done the hard work.

Just change the Unit

We just have to swap out the Units in the Expand and Move commands and trim any unnecessary commands.

Sub MoveWordLeft()
' Move the current word one word left
' Avoid any gotchas with a starting selection
Selection.Collapse

Selection.Expand Unit:=wdWord
Selection.Cut
Selection.MoveLeft Unit:=wdWord
Selection.Paste
End Sub

We eliminated the last end of the paragraph check since expanding by a doesn’t select the paragraph marker automatically like it does with sentences.

Again, we separate the move left and right macros since Word doesn’t allow parameters (in the parentheses) when running macros through the interface or assigning macros to keyboard shortcuts.

Sub MoveWordRight()
' Move the current word one word right
' Avoid any gotchas with a starting selection
Selection.Collapse

Selection.Expand Unit:=wdWord
Selection.Cut
Selection.MoveRight Unit:=wdWord
Selection.Paste
End Sub

I assigned my versions of these macros to Command+Option+Left or Right arrow in Word for Mac or Control+Alt+Shift+Left or Right on Windows.

The main thing I do different in my own version is to add an Undo record if needed, so it’s all undone as a single step, if need be, but that is a lesson for another day.

Affiliate Links

If you're interested in using Word or another tool related to the article, check out these affiliate links. I may make a small commission if you purchase when using them, but there is no increase in cost for you, and it helps to support this site and associated content.

I've been using Microsoft for Business for commercial use (that's us writers) on one of the lower pricing tiers for years. I get to use my macros, have online storage, and don't have to worry about software updates.