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.
Note that the StrReverse function also works with numbers.
For example, applying the StrReverse function to the number 1234 would return 4321.
Additional Resources
The following tutorials explain how to perform other common tasks in VBA:
VBA: How to Check if String Contains Another String
VBA: How to Count Number of Rows in Range
VBA: How to Write COUNTIF and COUNTIFS Functions