Reverse a string

BrianM

Well-known Member
Joined
Jan 15, 2003
Messages
768
Office Version
  1. 2016
Platform
  1. Windows
Is there a function that can reverse the contents of a cell?

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

Thanks,
Brian
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.

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.
 

BrianM

Well-known Member
Joined
Jan 15, 2003
Messages
768
Office Version
  1. 2016
Platform
  1. Windows
Thanks to you both.
 

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
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,167,965
Messages
5,856,555
Members
431,818
Latest member
Tori Murphy

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