Log in

View Full Version : Reversing word in a sentence



Dave T
04-26-2012, 12:50 AM
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)

Teeroy
04-26-2012, 01:35 AM
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:

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

Dave T
04-27-2012, 12:17 AM
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

Teeroy
04-27-2012, 12:54 AM
OUCH! Recursive functions hurt my brain :eek:

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 :


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



Hope this works for you.

Frosty
04-27-2012, 05:45 AM
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

Tinbendr
04-27-2012, 10:52 AM
Select the text in question, then run macro.
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

Dave T
04-29-2012, 04:43 PM
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

Teeroy
04-29-2012, 10:13 PM
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:

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


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.

Frosty
04-30-2012, 08:06 AM
You can remove any of those characters which don't help (such as a paragraph character, etc), by using the Replace function.


dim sTemp As String
sTemp = Replace(sTemp, vbcr, "")

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...

parse=split(replace(selection.text, vbcr, ""), ",")