20190310

Reverse String using Excel VBA

Normally this is an interview question being asked by company technical recruiters. This is an logical question and every aspirants must be prepared for these types of question while you are being interviewed for VBA Developer profile.

Let us try a simple method to retrieve the reverse string from the sentence provided.

Using below script you can create User defined function as well.

Code here:

Option Explicit
Sub GenerateReverseString()

'Declare Variables
Dim strText                 As String           'To Hold the value that you provide
Dim strReverseStrg     As String           'To Hold the reversed value
Dim intLength              As Integer         'Holds the length of string you provide
Dim intCtr                   As Integer         ' Counter

TextInput:

'Store string provided in Inputbox
     strText = InputBox("Enter the text you want to reverse")


'Determine text provided is numeric if yes jump back to Get input from user
    If IsNumeric(strText) Then
        MsgBox "Wrong Input, you have provided numbers"
        GoTo TextInput
    End If

'Determine the length of the text
     intLength = VBA.Len(strText)

'Now reverse loop thru the string
     For intCtr = 0 to intLength - 1
           strReverseStrg = _
                  strReverseStrg & VBA.Mid(strText, (intLength - intCtr), 1)
     Next intCtr

Msgbox strReverseStrg

End Sub







Subscribe to my blog for more articles



No comments:

Post a Comment

Search This Blog

Reverse String using Excel VBA

Normally this is an interview question being asked by company technical recruiters. This is an logical question and every aspirants must b...