VB or VBA code for permutations

L

Leisureman

Could you please help with writing a VB or VBA code for permutations:

Context:

There are some sentences in Word that will be selected by a user. The result
should be a permutation (only one, not all of them) of these sentences (i.e.,
sent4, sent2, sent1, sent3 instead of the initial sent1, sent2, sent3, sent4).
Thank you in advance for every help.
 
J

Jean-Guy Marcil

Leisureman was telling us:
Leisureman nous racontait que :
Could you please help with writing a VB or VBA code for permutations:

What have you got so far?
Context:

There are some sentences in Word that will be selected by a user. The

How do you define a "sentence" for your purpose?
result should be a permutation (only one, not all of them) of these

But in your example you permute more than one sentence. So, which is it?
sentences (i.e., sent4, sent2, sent1, sent3 instead of the initial
sent1, sent2, sent3, sent4). Thank you in advance for every help.

But, those questions become unimportant if the sentences selected by the
user are not contiguous (one after the other) because Word VBA does not
allow you to work with non-contiguous selection.


--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
J

Jay Freedman

Jean-Guy Marcil said:
Leisureman was telling us:
Leisureman nous racontait que :


What have you got so far?


How do you define a "sentence" for your purpose?


But in your example you permute more than one sentence. So, which is
it?


But, those questions become unimportant if the sentences selected by
the user are not contiguous (one after the other) because Word VBA
does not allow you to work with non-contiguous selection.

Once you've figured out what data you're working with, the general idea of
permuting it works like this:

- Create an array containing the data items (your sentences).
- For each index from 1 to the size of the array, select another index at
random and swap the contents at those two indexes.
- Print out the array in its new order.

Here's a sample macro:

Sub PermutationExample()
Dim MyArray As Variant
Dim Index As Integer
Dim OtherIndex As Integer
Dim TempStr As String
Dim msg As String

' initialize -- you'll have to do this differently
MyArray = Array("A", "B", "C", "D", "E", "F")
Randomize

' show starting state
For Index = 0 To UBound(MyArray)
msg = msg & MyArray(Index)
Next Index
MsgBox "Starting array: " & msg

' PERMUTE -- the important part!
For Index = 0 To UBound(MyArray)
' pick a random partner
OtherIndex = Rnd() * UBound(MyArray)

' swap it with the current entry
TempStr = MyArray(OtherIndex)
MyArray(OtherIndex) = MyArray(Index)
MyArray(Index) = TempStr
Next Index

' show ending state
msg = ""
For Index = 0 To UBound(MyArray)
msg = msg & MyArray(Index)
Next Index
MsgBox "Ending array: " & msg
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top