
Tips.Net > WordTips Home > Macros > Reversing a String
Summary: Need to reverse all the characters in a string? You can do so by using your own function that does the heavy lifting for you. (This tip works with Microsoft Word 97, Word 2000, Word 2002, Word 2003, and Word 2007.)
VBA includes some very powerful commands and functions for manipulating strings. As an example of that, consider the following VBA function, which reverses a string.
Function RevString(sRaw As String) As String
Dim sWork As String
Dim sTarget As String
Dim J As Integer
sWork = sRaw
sTarget = ""
For J = 1 To Len(sWork)
sTarget = Mid(sWork, J, 1) & sTarget
Next J
RevString = sTarget
End Function
It works by pulling the original string apart and putting it back together in the new order. The function returns a reversed version of whatever string you send it. For example, take a look at this VBA code to use the function:
Dim MyString As String Dim BackString As String MyString = "ABCD1234" BackString = RevString(MyString)
When the code snippet is through executing, the value of BackString is set to 4321DCBA, which is the reverse of the original value of MyString.
Tip #767 applies to Microsoft Word versions: 97 2000 2002 2003 2007
Take Control! Experienced users know that styles can make the difference between a plain document and a masterful one. This is the real power behind Word, and the key to that power can be found in WordTips: Styles and Templates.
If you provide support and help to others, you need the power offered in the WordTips Ten-Year Library. There is no better information repository anywhere! (more information...)
Ask a Word Question
Make a Comment
Beauty Tips
Bugs and Pests Tips
Car Tips
Cleaning Tips
College Tips
Cooking Tips
Excel2007 Tips
ExcelTips
Family Tips
Gardening Tips
Health Tips
Home Tips
Money Tips
Organizing Tips
Pet Tips
Word2007 Tips
WordTips