VBA Tip: Reversing a String

To reverse a string in VBA, there's no need to manually loop through it as there is already a function for that!

It's called StrReverse.



Simple Example of Reversal

Reversing a first name using the StrReverse function:

Sub reverse()

    firstName = "John" 'Value: John
    reversed = StrReverse(firstName) 'Value: nhoJ
End Sub

Palindrome Function

A palindrome is a word or phrase that reads the same forwards and backwards.

To check if a string is a palindrome, you simply need to compare it with its reversed value.

Here's a simple function that tests this and returns True if it's a palindrome or False otherwise:

Function palindrome(value)
    palindrome = StrReverse(value) = value
End Function

If you want to use this function in an Excel worksheet, use this version instead (and place it in a module):

Function PALINDROME(value)
    PALINDROME = StrReverse(value) = value
End Function
excel palindrome function strreverse