Reverse a string

BrianM

Well-known Member
Joined
Jan 15, 2003
Messages
764
Is there a function that can reverse the contents of a cell?

i.e. A1=Cat ...result = tac

Thanks,
Brian
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
Or a User Defined function

Code:
Function reverse(S As String)
    Dim Temp As String, I As Integer, n As Integer
    n = Len(S)
    For I = n To 1 Step -1
        Temp = Temp & Mid(S, I, 1)
    Next I
    reverse = Temp & Right(S, Len(S) - n)
End Function

ASAP-Utilities is better though unless you are going to be doing this a lot.
 

erik.van.geit

MrExcel MVP
Joined
Feb 1, 2003
Messages
17,832

ADVERTISEMENT

Hi,

there is an inbuiltfunction
might be a more recent one
Code:
MsgBox StrReverse("cat")
kind regards,
Erik
 

DominicB

Well-known Member
Joined
Oct 3, 2005
Messages
1,569
Good evening BrianM

This would do the trick :

Code:
Function Reverse(Backwards As String)
Dim Length, Pos As Integer
Reverse = ""
Length = Len(Backwards)
For Pos = Length To 1 Step -1
Reverse = Reverse & Mid(Backwards, Pos, 1)
Next Pos
End Function

My add-in, available from the link below has this as a function, so once installed =REVERSE(A1) would show tac (if A1 contained "cat")

HTH

DominicB
 

erik.van.geit

MrExcel MVP
Joined
Feb 1, 2003
Messages
17,832

ADVERTISEMENT

talking about functions:
Code:
Function reverse(S As String)
    reverse = StrReverse(S)
End Function
so no need to loop
as mentioned, check if it works for your version
 

Todd Bardoni

Well-known Member
Joined
Aug 29, 2002
Messages
3,042
Using formulas, (not too pretty though):-
Book1
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABAC
1ToddBardoniinodraB ddoTTTTTTTTTTTTTTTinodraBddoT
Sheet1

In C1:AB1
Code:
=LEFT(RIGHT(Sheet1!$A$1,COLUMN(1:$256)),1)
Confirmed w/ Ctrl+Shift+Enter

In AC1
Code:
=LEFT(C1&D1&E1&F1&G1&H1&I1&J1&K1&L1&M1&N1&O1&P1&Q1&R1&S1&T1&U1&V1&W1&X1&Y1&Z1&AA1&AB1,LEN($A$1))
 

DominicB

Well-known Member
Joined
Oct 3, 2005
Messages
1,569
Hi Erik

That "StrReverse" commands a handy thing to know. It would be interesting to know when it was introduced. As far back as XL2000...?

DominicB
 

erik.van.geit

MrExcel MVP
Joined
Feb 1, 2003
Messages
17,832
Domonic,

I'll check it out on an elder PC with XL2000.
Almost sure it wasn't available in XL97

best regards,
Erik
 

Watch MrExcel Video

Forum statistics

Threads
1,129,800
Messages
5,638,425
Members
417,025
Latest member
MusterDuster

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top