How to Reverse a String in VBA (With Example)


You can use the StrReverse function in VBA to reverse a text string.

Here is one common way to use this function in practice:

Sub ReverseStrings()
    
Dim i As Integer

For i = 2 To 11
    Range("B" & i) = StrReverse(Range("A" & i))
Next i

End Sub

This particular example reverses each string in the range A2:A11 and displays the results in the range B2:B11.

The following example shows how to use this syntax in practice.

Example: How to Reverse Strings Using VBA

Suppose we have the following column of basketball team names in Excel:

Suppose we would like to reverse each team name and display the results in the corresponding cell in column B.

We can create the following macro to do so:

Sub ReverseStrings()
    
Dim i As Integer

For i = 2 To 11
    Range("B" & i) = StrReverse(Range("A" & i))
Next i

End Sub

When we run this macro, we receive the following output:

Column B displays each team name from column A in reverse.

For example:

  • Mavs becomes svaM
  • Spurs becomes srupS
  • Rockets becomes stekcoR
  • Kings becomes sgniK

And so on.

For example, applying the StrReverse function to the number 1234 would return 4321.

x