Quick Jump
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 abc becomes cba.


Example - VBA StrReverse

Sub ReverseStringSub()
str1 = "abcdef"
str1 = StrReverse(str1)
Debug.Print str1
End Sub

Get wellsrPRO for Free

Get these macros delivered right to your spreadsheet for free

Access tutorials and import my macros without ever leaving Excel

Show me - It's free

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 str1 from

abcdef

to

fedcba

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(A1) into any cell in Excel, like this:

VBA StrReverse Reverses the Order of a String in Excel
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.

Comments

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 Google+ and Twitter for even more great VBA content!


Get wellsrPRO for Free

Get these macros delivered right to your spreadsheet for free

Access tutorials and import my macros without ever leaving Excel

Show me - It's free

About Ryan Wells


Ryan Wells

Ryan Wells is a Nuclear Engineer and professional VBA Developer. He is the lead developer of several VBA applications, including PDF VBA - a leading Excel Add-in for exporting Excel Objects, like charts and tables, to PDFs. Discover more of his popular Excel Add-ins, including Mouse To Macro and CF Shapes, at his dedicated Excel Add-ins page.