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

Write better macros in half the time
I see people struggling with Excel every day and I want to help. That's why I'm giving away my personal macro library for free. This powerful gift lets you automatically import all my macros directly into your spreadsheet with just one click.

Show me how I can improve my VBA skills for 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.


Write better macros in half the time
I see people struggling with Excel every day and I want to help. That's why I'm giving away my personal macro library for free. This powerful gift lets you automatically import all my macros directly into your spreadsheet with just one click.

Show me how I can improve my VBA skills for free

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