Reversing a String

Written by Allen Wyatt (last updated April 30, 2021)
This tip applies to Word 2007, 2010, 2013, and 2016


3

VBA includes some very powerful commands and functions for manipulating strings. One such function allows you to easily reverse the contents of a string: the StrReverse function. All you need to do is to pass it a string value, and it returns the reversed version of the string:

Dim MyString As String
Dim BackString As String

MyString = "ABCD1234"
BackString = StrReverse(MyString)

When the code snippet is through executing, the value of the BackString variable is set to 4321DCBA, which is the reverse of the original value of the MyString variable.

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 (11514) applies to Microsoft Word 2007, 2010, 2013, and 2016.

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

Adding Automatic Lines

Want an easy way to add lines in your document? You can do it by making sure Word is using one of its AutoFormat features.

Discover More

Cleaning Text

You can use the CLEAN worksheet function to remove any non-printable characters from a cell. This can come in handy when ...

Discover More

Using Multiple References to a Single Comment

Find yourself repeating the same comment over and over? Here's a couple of ways you can save some typing by simply ...

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)

Moving the Insertion Point to the End of a Line

When writing a macro to process the text in a document, you may need to move the insertion point to the end of a line. ...

Discover More

Finding and Changing Word's Internal Commands

If you know how to create macros, you can easily create entire replacements for Word's internal commands. Here's all you ...

Discover More

Deriving an Absolute Value

Want to know the absolute value of a number? It's easy to derive in VBA by using the Abs function.

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 1 + 4?

2017-02-28 06:10:06

Alan Elston

Chris,
I am not quite following exactly what you are wanting to do, but some thoughts ..
I only recently noticed that there is a StrReverse-Funktion. ( Documentation in VBA is scarce, one seems to find info on this if you look under Visual Basic )

You mentioned doing a quite laboriously splitting it into digits in Excel. ??
Be aware that usually any function designed to take a string will happily take a number
The code snippet below “works” to reverse a number
What slightly surprised me initially here is that I see that I can also apply the returned value from StrReverse to a number variable.
( Possibly I should not have been surprised by that as the last line in the code snippet also works )

The code works in Excel or Word

If you are in Word, you can add the code line from John Summers,
Let Nmbr = Selection.Text
Then as long as you have selected numbers in a word document, then the code will also work

Sub RevStr() ' https://wordribbon.tips.net/T011514_Reversing_a_String.html
Dim Nmbr As Long: Let Nmbr = 1234
'Let Nmbr = Selection.Text' For use in Word if you select only numbers
Dim RevNmbr As Long

Let RevNmbr = StrReverse(Nmbr)
MsgBox prompt:="Reversed Number value is " & RevNmbr & " , the variable type retuned is " & TypeName(StrReverse(Nmbr)) & " , and the final type of the reversed number is " & TypeName(Nmbr)

Dim strNmbr As String: Let strNmbr = "1234"
Let RevNmbr = StrReverse(strNmbr)

Let Nmbr = strNmbr
End Sub


Alan


2017-02-27 06:24:44

John Summers

Chris,

This may help. Select your number sequence and run this macro. (Thanks and apologies to Allen for basing this on his example.)

Sub Reverse_String()

Dim MyString As String
Dim BackString As String

MyString = Selection.Text
BackString = StrReverse(MyString)

MsgBox BackString

End Sub


2017-02-26 01:59:37

Chris Finn

I sometimes have a series of numbers which need reversing (generally when I am attempting to solve a mathematical puzzle).
Hence I'd appreciate a way of applying this tip to a number, and having the answer as a number also.
Hitherto I have been laboriously splitting it into digits and reassembling them in reverse order, using Excel.
Many thanks.
Chris F


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.