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

Turning Off Track Changes without Unsharing

The Track Changes tool in Excel can be helpful, but it can also be aggravating because it doesn't allow you to use it on ...

Discover More

Improving Text Sharpness with ClearType

Ever notice that even with a fancy new monitor, text can still look a bit muddy on the screen. You can use ClearType ...

Discover More

Capturing a Screen

Windows provides a way of "capturing" the image on the screen into the clipboard. You can then paste the image from the ...

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)

Passing Parameters to Functions

Functions can be used to perform repetitive tasks and return values to your main program. You can also pass values to a ...

Discover More

Determining Differences Between Dates

Do you need to do some simple math using dates in a your macro? One of the easy functions you can use is the DateDiff ...

Discover More

Comparing Strings

When writing a macro, a common task is the need to compare two strings. You can do this by "normalizing" the strings, as ...

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 eight more than 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.