Results 1 to 3 of 3

Thread: Reverse a string by using array method

  1. #1

    Smile Reverse a string by using array method

    This the code by which you are able to reverse a string by using array method



    Code:
    Sub Reverse_String_Using_Array()
        Dim strString       As String
        Dim lngCount        As Long
        Dim strReverse      As String
        Dim Arr()
        
        strString = Application.InputBox("Enter your string which you want to reverse")
        
        For lngCount = 1 To VBA.Len(strString)
            ReDim Preserve Arr(lngCount)
            Arr(lngCount) = VBA.Mid(strString, lngCount, 1)
        
        Next lngCount
        
        For lngCount = UBound(Arr) To LBound(Arr) Step -1
            strReverse = strReverse & Arr(lngCount)
        Next lngCount
        
        MsgBox strReverse
        
        Erase Arr
        
    End Sub

  2. #2
    Administrator Admin's Avatar
    Join Date
    Mar 2011
    Posts
    1,123
    Rep Power
    10
    Hi

    VBA has an in-built function StrReverse to do the same.

    Also, you don't need 2 loops to get the result. You can do the same like

    Code:
    For lngCount = Len(strString) To 1 Step -1
            strReverse = strReverse & Mid$(strString, lngCount, 1)
        Next
    Cheers !

    Excel Range to BBCode Table
    Use Social Networking Tools If You Like the Answers !

    Message to Cross Posters

    @ Home - Office 2010/2013/2016 on Win 10 (64 bit); @ Work - Office 2016 on Win 10 (64 bit)

  3. #3
    Hi Admin,

    Thanks for your response. I have aware with StrReverse function but try to write VBA code without using the help of StrReverse function.

    Cheers!!! :D

Similar Threads

  1. Reverse Vlookup Using Choose Function
    By Excel Fox in forum Excel Help
    Replies: 8
    Last Post: 07-04-2013, 01:50 AM
  2. Concatenate array string
    By tushar.tarafdar in forum Excel Help
    Replies: 2
    Last Post: 09-20-2012, 12:00 PM
  3. Replies: 10
    Last Post: 04-07-2012, 05:33 AM
  4. WorksheetFunction.MInverse Method
    By Rasm in forum Excel Help
    Replies: 1
    Last Post: 12-06-2011, 07:55 AM
  5. Worksheet Protection Method with UserInterfaceOnly Argument
    By technicalupload in forum Download Center
    Replies: 1
    Last Post: 09-02-2011, 04:17 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •