Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 05-10-2016, 04:37 AM
macdadi112 macdadi112 is offline Shuffling a sentence Windows 7 64bit Shuffling a sentence Office 2010 64bit
Novice
Shuffling a sentence
 
Join Date: May 2016
Posts: 6
macdadi112 is on a distinguished road
Unhappy 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
Reply With Quote
  #2  
Old 05-10-2016, 02:09 PM
macropod's Avatar
macropod macropod is offline Shuffling a sentence Windows 7 64bit Shuffling a sentence Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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
Simply click anywhere in the sentence and run the macro. Do note, though, that a VBA sentence is not the same as a grammatical sentence - periods used in abbreviations, for example, get interpreted by VBA as sentence ends.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 05-10-2016, 02:25 PM
macdadi112 macdadi112 is offline Shuffling a sentence Windows 7 64bit Shuffling a sentence Office 2010 64bit
Novice
Shuffling a sentence
 
Join Date: May 2016
Posts: 6
macdadi112 is on a distinguished road
Default

Thanks the above works..

So what if I wanted it to run in every sentence in the file. Something like a for loop..
Reply With Quote
  #4  
Old 05-10-2016, 02:47 PM
macropod's Avatar
macropod macropod is offline Shuffling a sentence Windows 7 64bit Shuffling a sentence Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #5  
Old 05-11-2016, 03:17 AM
macdadi112 macdadi112 is offline Shuffling a sentence Windows 7 64bit Shuffling a sentence Office 2010 64bit
Novice
Shuffling a sentence
 
Join Date: May 2016
Posts: 6
macdadi112 is on a distinguished road
Default

Quote:
Originally Posted by macropod View Post
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.
Thank you.
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shuffling a sentence End of sentence puncuation. nrsmd Word 2 07-04-2015 10:33 PM
Shuffling a sentence Delete does not bring second sentence closer to first sentence Andoheb Word 29 07-03-2014 01:48 PM
Shuffling a sentence 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
Shuffling a sentence skip to next sentence? moreenz Word 3 08-16-2012 02:41 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 01:08 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft