Written by Allen Wyatt (last updated April 30, 2021)
This tip applies to Word 2007, 2010, 2013, and 2016
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:
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.
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!
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 MoreIf you know how to create macros, you can easily create entire replacements for Word's internal commands. Here's all you ...
Discover MoreWant to know the absolute value of a number? It's easy to derive in VBA by using the Abs function.
Discover MoreFREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
2017-02-28 06:10:06
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
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.
Visit the WordTips channel on YouTube
FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2023 Sharon Parq Associates, Inc.
Comments