PDA

View Full Version : Reverse a string by using array method



hanishgautam
07-08-2013, 09:17 PM
This the code by which you are able to reverse a string by using array method




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

Admin
07-08-2013, 10:15 PM
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


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

hanishgautam
07-09-2013, 09:07 AM
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