Consulting

Results 1 to 9 of 9

Thread: Reversing word in a sentence

  1. #1
    VBAX Contributor
    Joined
    Mar 2007
    Posts
    140
    Location

    Reversing word in a sentence

    Hello All,

    I use a program that generates a list of names of a road route separated by commas.
    Is there a macro that can reverse order of the words within a selected sentence.

    For example:
    LINCOLN HWY, BROADBENT TCE, PLAYFORD AVE, MCBRYDE TCE, NORRIE AVE, WHYALLA

    when reversed becomes:
    WHYALLA, NORRIE AVE, MCBRYDE TCE, PLAYFORD AVE, BROADBENT TCE, LINCOLN HWY

    Any help appreciated.

    Regards,
    Dave T
    (now using Word 2007)

  2. #2
    VBAX Mentor Teeroy's Avatar
    Joined
    Apr 2012
    Location
    Sydney, Australia
    Posts
    414
    Location
    I wrote and tested this in VBA for Excel but I don't see that there would be any difference in VBA for Word.

    Code:
    [vba]
    Option Explicit
    Sub reverse()


    Dim str As String
    Dim reverse_str As String
    Dim parse As Variant
    Dim i As Integer


    str = "LINCOLN HWY, BROADBENT TCE, PLAYFORD AVE, MCBRYDE TCE, NORRIE AVE, WHYALLA"

    parse = Split(str, ",")

    reverse_str = ""

    For i = UBound(parse) To LBound(parse) Step -1
    reverse_str = reverse_str & ", " & Trim(parse(i))
    Next i

    'remove the leading comma
    reverse_str = Right$(reverse_str, Len(reverse_str) - 1)
    MsgBox reverse_str

    End Sub
    [/vba]

  3. #3
    VBAX Contributor
    Joined
    Mar 2007
    Posts
    140
    Location
    Hello Teeroy,

    Thanks for the reply.

    I found a macro written by macropod on the net which works, in that it reverses the order of the words, but it also reverses the letters of each word and puts the commas in the wrong place.
    Basically it just reverses everything.

    E.g.: ALLAYHW ,EVA EIRRON ,ECT EDYRBCM ,EVA DROFYALP ,ECT TNEBDAORB ,YWH NLOCNIL

    Option Explicit
     
    Sub ReverseString()
      'http://us.generation-nt.com/answer/reversing-word-help-164698401.html
      Selection.TypeText Reverse(Selection)
    End Sub
    
    Function Reverse(Selection As String) As String
      If (Len(Selection) > 1) Then
        Reverse = Reverse(Mid$(Selection, 2)) + Left$(Selection, 1)
      Else
        Reverse = Selection
      End If
    End Function
    I was hoping to be able to select the text as macropod's code allows, but it just reverses to order of the words between the commas and keep muliple words between the commas in their correct order i.e. still be LINCOLN HWY and not HWY LINCOLN

    Regards,
    Dave T

  4. #4
    VBAX Mentor Teeroy's Avatar
    Joined
    Apr 2012
    Location
    Sydney, Australia
    Posts
    414
    Location
    OUCH! Recursive functions hurt my brain

    That function recursively reverses the order of the string by character. To do what you want you need to parse on the ",". I've re-written what I proposed earlier as a function using some of the example you provided.

    In function form :

    [vba]
    Sub Reverse_Selection()


    Selection.TypeText Reverse(Selection)


    End Sub


    Function Reverse(str As String) As String
    parse = Split(str, ",")
    For i = UBound(parse) To LBound(parse) Step -1
    If i = 1 Then
    Reverse = Trim(parse(i))
    Else
    Reverse = Reverse & ", " & Trim(parse(i))
    End If
    Next i
    End Function

    [/vba]

    Hope this works for you.

  5. #5
    VBAX Master
    Joined
    Feb 2011
    Posts
    1,480
    Location
    Haha... Recursive functions hurt my head too! But they can be SO useful sometimes.

    I would have approached it exactly the same way as teeroy

  6. #6
    VBAX Expert Tinbendr's Avatar
    Joined
    Jun 2005
    Location
    North Central Mississippi (The Pines)
    Posts
    993
    Location
    Select the text in question, then run macro.
    [vba]Sub reverse()

    Dim reverse_str As String
    Dim parse As Variant
    Dim i As Integer


    reverse_str = Selection.Range
    If Len(reverse_str) <> 0 Then
    parse = Split(reverse_str, ",")

    reverse_str = ""

    For i = UBound(parse) To LBound(parse) Step -1
    reverse_str = reverse_str & ", " & Trim(parse(i))
    Next i

    'remove the leading comma
    reverse_str = Right$(reverse_str, Len(reverse_str) - 1)
    Selection.Range = reverse_str
    End If
    End Sub

    [/vba]

    David


  7. #7
    VBAX Contributor
    Joined
    Mar 2007
    Posts
    140
    Location
    Hello Teeroy and Tinbendr,

    Thank you very much for you replies.

    When I ran Teeroy's code, I selected the text to be reversed and for some reason nothing was reversed however all text was deleted except for "NORIE AVE, WHYALLA".

    When I ran Tinbendr's code, it does reverse the sentence, but puts WHYALLA on a line (paragraph) by itself with a space before WHYALLA.
    The rest of the sentence is in the next paragraph and the following paragraph after that is now joined to the next sentence, i.e.

    WHYALLA
    , NORRIE AVE, MCBRYDE TCE, PLAYFORD AVE, BROADBENT TCE, LINCOLN HWYLINCOLN HWY, BROADBENT TCE, PLAYFORD AVE, MCBRYDE TCE, NORRIE AVE, WHYALLA
    Regards,

    Dave T

  8. #8
    VBAX Mentor Teeroy's Avatar
    Joined
    Apr 2012
    Location
    Sydney, Australia
    Posts
    414
    Location
    Hi Dave T,

    My apologies I changed tack while cleaning up my test code and inadvertantly left a remnant. Where there is a test for i = 1 it should test for the first iteration i.e. i = Ubound(Parse).


    The code is therefore:
    [VBA]
    Sub Reverse_Selection()


    Selection.TypeText Reverse(Selection)


    End Sub


    Function Reverse(Selection As String) As String
    parse = Split(Selection, ",")

    For i = UBound(parse) To LBound(parse) Step -1

    If i = UBound(parse) Then
    Reverse = Trim(parse(i))
    Else
    Reverse = Reverse & ", " & Trim(parse(i))
    End If
    Next i
    End Function
    [/VBA]

    I believe the reason that Tinbendr's code is causing a split line is that you're collecting a vbCrLf attached to end of the paragraph in your selection. Mine will do the same.
    _________________________________________________________________________
    "In theory there is no difference between theory and practice. In practice there is." - Chuck Reid

    Any day you learn something new is a day not wasted.

  9. #9
    VBAX Master
    Joined
    Feb 2011
    Posts
    1,480
    Location
    You can remove any of those characters which don't help (such as a paragraph character, etc), by using the Replace function.

    [vba]
    dim sTemp As String
    sTemp = Replace(sTemp, vbcr, "")
    [/vba]
    If you want to be really advanced and skip the intermediary step and using a variable, then you could do it at the same time as using the split... a la...
    [VBA]
    parse=split(replace(selection.text, vbcr, ""), ",")
    [/vba]

Posting Permissions

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