Share via

A VBA ( Macro code) to reverse a text!

Anonymous
2010-07-22T14:24:00+00:00

Does anybody know a VBA (Macro code) which be able to reverse a text

For example changes the text above to:

txet a esrever ot elba eb hcihw )edoc orcaM( ABV a wonk ydobyna seoD

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2010-07-22T14:35:53+00:00

How about

Dim oRng As Range

Dim sRevText As String

Dim i As Long

sRevText = ""

Set oRng = Selection.Range

For i = oRng.Characters.Count To 1 Step -1

    sRevText = sRevText & oRng.Characters(i)

Next i

oRng.Text = sRevText

Select the text and run the macro.

<McKazie> wrote in message news:*** Email address is removed for privacy ***...

Does anybody know a VBA (Macro code) which be able to reverse a text

For example changes the text above to:

txet a esrever ot elba eb hcihw )edoc orcaM( ABV a wonk ydobyna seoD


Graham Mayor - Word MVP

www.gmayor.com

Posted via the Communities Bridge

http://communitybridge.codeplex.com/

Was this answer helpful?

3 people found this answer helpful.
0 comments No comments

Answer accepted by question author

Jay Freedman 207.7K Reputation points Volunteer Moderator
2010-07-22T15:32:55+00:00

As a macro for Word instead of Excel,

Sub ReverseWords()

    Dim i As Long

    Dim OldString As Variant

    Dim RevText As Variant

    OldString = Split(Selection.Text, " ")

    For i = 0 To UBound(OldString)

        OldString(i) = StrReverse(OldString(i))

    Next

    RevText = Join(OldString, " ")

    MsgBox RevText

End Sub


Jay Freedman

MS Word MVP  FAQ: http://word.mvps.org

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

7 additional answers

Sort by: Most helpful
  1. Anonymous
    2010-07-22T14:54:25+00:00

    NewText = StrReverse(OriginalText)

    --

    Enjoy,

    Tony

    "McKazie" wrote in message

    news:*** Email address is removed for privacy ***...

    > Does anybody know a VBA (Macro code) which be able to reverse a text

    >

    > For example changes the text above to:

    >

    >

    >

    > txet a esrever ot elba eb hcihw )edoc orcaM( ABV a wonk ydobyna seoD

    >

    >

    >


    Enjoy,

    Tony

    www.WordArticles.com

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2010-07-22T14:52:03+00:00

    Mike, I think you are going to kick yourself when you see this...

    Function RevText(Rcell As String) As String

        RevText = StrReverse(Rcell)

    End Function

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2010-07-22T14:40:47+00:00

    Hi,

    ALT+F11 to open VB editor. Right click ThisWorkbook and insert module and paste the code below in

    back on the worksheet call with

    =RevText(a1)

    Where A1 contains the text to reverse

    Function RevText(Rcell As Range)

    Dim i As Long

    Dim NewString As String

    Dim OldString As String

    OldString = Trim(Rcell)

    For i = 1 To Len(OldString)

        NewString = Mid(OldString, i, 1) & NewString

    Next i

    RevText = NewString

    End Function


    If this post answers your question, please mark it as the Answer.

    Mike H

    Was this answer helpful?

    0 comments No comments