#1
|
|||
|
|||
Shuffling a sentence
Hi,
I want to create a macro to be able to shuffle each sentence. For example: this is a test sentence Liverpool is a city found in the uk ... .. . to sentence is this a city is a uk in the Liverpool found Your help would be appreciated. Thanks Mac |
#2
|
||||
|
||||
For that, you could use a macro like:
Code:
Sub Demo() Randomize Timer Dim StrTxt As String, StrTmp As String, StrOut As String Dim i As Long, j As Long With Selection.Range .Start = .Sentences.First.Start .End = .Sentences.First.End While .Characters.Last Like "[.!:;?" & vbCr & vbTab & vbLf & "]" .End = .End - 1 Wend StrTxt = " " & Trim(.Text) & " " While Len(Trim(StrTxt)) > 1 i = UBound(Split(StrTxt, " ")) + 1 j = Int(Rnd * i) If j > 0 Then StrTmp = Split(StrTxt, " ")(j) & " " StrOut = Trim(StrOut & " " & StrTmp) StrTxt = Replace(StrTxt, " " & StrTmp, " ", 1, 1) End If Wend .Text = StrOut End With End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
Thanks the above works..
So what if I wanted it to run in every sentence in the file. Something like a for loop.. |
#4
|
||||
|
||||
Yes, a loop could be used (though in that case I wouldn't suggest working with selections) but, as I also said, VBA has no idea what a grammatical sentence is. For example, consider the following:
Mr. Smith spent $1,234.56 at Dr. John's Grocery Store, to buy: 10.25kg of potatoes; 10kg of avocados; and 15.1kg of Mrs. Green's Mt. Pleasant macadamia nuts. For you and me, that would count as one sentence; for VBA it counts as 5... Consequently, the VBA 'shuffle' would treat it as 5 sentences that would each be shuffled only within that VBA 'sentence' range.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
|||
|
|||
Quote:
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
End of sentence puncuation. | nrsmd | Word | 2 | 07-04-2015 10:33 PM |
Delete does not bring second sentence closer to first sentence | Andoheb | Word | 29 | 07-03-2014 01:48 PM |
First word of sentence IF and then | awolf | Word VBA | 7 | 03-16-2014 02:40 PM |
Space Between Each Sentence | dazwm | Word | 2 | 10-17-2012 04:19 AM |
skip to next sentence? | moreenz | Word | 3 | 08-16-2012 02:41 PM |