Introduction | Example | Tutorial | Applications | Comments
Introduction - VBA StrReverse
The VBA StrReverse function reverses the order of a string in Excel. In other words, StrReverse writes your string backward so the string
Example - VBA StrReverse
Sub ReverseStringSub() str1 = "abcdef" str1 = StrReverse(str1) Debug.Print str1 End Sub
Becoming a VBA expert isn't hard
Over 5000 members are improving their VBA skills for free with our email tutorials. Why don't you join them? Our experts share time-saving VBA tips and we'll give you access to our huge macro library - it's sure to speed up your macro development.
Tutorial - VBA StrReverse
The macro above illustrates how the VBA StrReverse function takes a string and writes it in the reverse order. It changes the string
Believe it or not, it’s actually a useful feature. I used the StrReverse function to replace last occurrence of substring in a string in my article about the VBA Replace function.
Excel Function to Reverse a String
Excel doesn’t natively have an easy way reverse a string, but it’s easy to make your own function! Just copy and paste the following UDF into your Visual Basic editor:
Function ReverseString(v1 As Variant) ReverseString = StrReverse(Trim(v1)) End Function
Now, anytime you want to reverse a string in Excel, you have a function to do it. If you want to reverse the order of the string in cell A1, just type
=ReverseString( into any cell in Excel, like this:
The Excel string is inverted!
The beauty of passing this function a variant is that it can reverse a lot more than just strings! It can reverse dates and numbers, too.
Application Ideas - VBA StrReverse
Reversing a string is great if you’re combining it with other string manipulation functions. I already showed you how I combined StrReverse with the VBA Replace function to replace last occurrence of substring in a string.
You don’t have to limit yourself to using this function in Excel, though. You can use it in Microsoft Word, too! If for some reason you want to flip the order of an entire document, you can certainly do that.
I want to thank all my readers that have already subscribed to my email list and I encourage you to go ahead and subscribe if you haven’t done so. What are you waiting for? You’ll love the great VBA content I send your way!
Let’s have a discussion in the comments section.
Share this article on social media and follow me on Twitter for even more great VBA content!