Reversing a String

by Allen Wyatt
(last updated February 25, 2017)

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.

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

Inserting Fields

Fields are used for a variety of dynamic purposes in a document. There are a couple of ways you can easily insert fields, as ...

Discover More

Deciphering a Coded Date

It is no secret that Excel allows you to work with dates in your worksheets. Getting your information into a format that is ...

Discover More

Determining If a Date is between Other Dates

Need to figure out if one date is between two other dates? There are a wide variety of formulaic approaches you could use to ...

Discover More

Learning Made Easy! Quickly teach yourself how to format, publish, and share your content using Word 2013. With Step by Step, you set the pace, building and practicing the skills you need, just when you need them! Check out Microsoft Word 2013 Step by Step today!

More WordTips (ribbon)

Locating the My Documents Folder

Need to do some macro processing of documents in the user's My Documents folder? First step is to figure out where the folder ...

Discover More

Understanding the If ... End If Structure

One of the powerful programming structures provided in VBA allows you to conditionally execute commands. The If ... End If ...

Discover More

Running Macros Based on Keywords

Wouldn't it be great if Word could execute a macro every time someone typed in a particular keyword or phrase? Word may not ...

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}] 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 6 + 0?

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.

Newest Tips
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.