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

Accessing Paragraphs in a Macro

Need to process a document, paragraph by paragraph, in a macro? It's easy to do once you understand that Word's object model ...

Discover More

Converting Tables to Text

Need to convert all the tables in your document into plain text? This tip provides a macro that can make quick work of a ...

Discover More

Concatenating Ranges of Cells

Putting the contents of two cells together is easy. Putting together the contents of lots of cells is more involved, as ...

Discover More

Do More in Less Time! Are you ready to harness the full power of Word 2013 to create professional documents? In this comprehensive guide you'll learn the skills and techniques for efficiently building the documents you need for your professional and your personal life. Check out Word 2013 In Depth today!

More WordTips (ribbon)

Understanding Subroutines

The heart of creating powerful programs in VBA is to understand how to create subroutines. These structures allow you to ...

Discover More

Creating a New Document in VBA

When working with documents in a macro, it makes sense that you may need to create a document from time to time. Here's how ...

Discover More

Controlling Repagination in Macros

Want to turn off document repagination while your macro does its work? Here are two approaches you can use.

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 8Mpixels. 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 4 + 7?

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.