In this post, you will learn how to reverse a string in your excel spreadsheet using Excel VBA with an example.
Reverse string in Excel VBA
To reverse a string, follow these steps.
- First, insert a command button.
- Enter the following code.
Code:
Private Sub CommandButton1_Click() Dim String1 As String Dim StringReverse As String Dim Char As String Dim Size As Integer Dim Point As Integer String1 = InputBox("String1: ") Size = Len(String1) StringReverse = "" For Point = Size To 1 Step -1 Char = Mid(String1, Point, 1) StringReverse = StringReverse & Char Next Point MsgBox "String reversed: " & StringReverse End Sub
Click on the run button or click the command button. An input dialog box opens, enter the text and click okay.
A message box with the reversed string appears.